def init_app(): """ Factory function to init Flask applictaion. """ # Init Flask application and necessary extensions app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' admin = Admin(app) db = SQLAlchemy(app) # Add url rules to application views = LazyViews(app) views.add('/', 'views.home') views.add('/db', 'views.database_page') views.add('/error', 'views.server_error') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') views.add_admin('admin.AdminView', endpoint='app_admin', name='App View') views.add_error(404, 'views.error') views.add_error(500, 'views.error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='favicon') # Register test blueprint app.register_blueprint(test_blueprint, url_prefix='/test') return (app, admin, db)
def create_app(name=None, **options): """ Factory function to create Flask applictaion. """ # Init Flask application and configure it app = Flask(name or __name__) app.config.update({'DEBUG': True, 'SQLALCHEMY_DATABASE_URI': 'sqlite://'}) app.config.update(options) # Setup necessary extensions Admin(app), SQLAlchemy(app) # Add lazy views to application views = LazyViews(app, 'testapp') views.add('/', 'views.home') views.add('/db', 'views.database_page', endpoint='dbpage') views.add('/error', 'views.server_error') views.add('/error/default', 'views.custom_error', defaults={'code': 400}) views.add('/error/<int:code>', 'views.custom_error') views.add('/gone', 'views.custom_error', defaults={'code': 410}, endpoint='gone') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') # Admin view views.add_admin('admin.AdminView', endpoint='app_admin', name='Custom Admin Page') # Error handlers views.add_error(404, 'views.error') views.add_error(500, 'views.error') # Custom static file serving views.add_static('/favicon.ico', 'img/favicon.ico', endpoint='favicon') views.add_static('/old-favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='old_favicon') # Render templates with dict/callable context or without any context views.add_template('/template', 'template.html', context={'text': 'Test Text'}, endpoint='template') views.add_template('/template/callable-context', 'template.html', context=lambda: {'text': 'Callable Test Text'}, endpoint='template_callable_context') views.add_template('/template/no-context', 'template.html', endpoint='template_no_context') # Create and register test blueprint app.register_blueprint(create_blueprint(), url_prefix='/test') return app
def create_blueprint(name=None): """ Factory for create blueprint. """ # Create blueprint name = name or 'testblueprint' blueprint = Blueprint(name, 'testapp.{0}'.format(name), template_folder='templates') # Initialize lazy views extension, register views and error handlers views = LazyViews(blueprint, import_prefix='.views') views.add('/', 'test') views.add('/advanced', 'advanced', methods=('GET', 'POST')) views.add('/error/<int:code>', 'custom_error') views.add('/gone', 'custom_error', defaults={'code': 410}, endpoint='gone') views.add_error(403, 'error') views.add_error(410, 'error', app=True) return blueprint
import sys from flask import Flask from flask.ext.admin import Admin from flask.ext.lazyviews import LazyViews from flask.ext.script import Manager from testapp.test import blueprint as test_blueprint # Init Flask application and necessary extensions app = Flask(__name__) admin = Admin(app) manager = Manager(app) # Add url rules to application views = LazyViews(app) views.add('/', 'views.home') views.add('/error', 'views.server_error') views.add('/page/<int:page_id>', 'views.page', endpoint='flatpage') views.add('/page/<int:page_id>/cls', 'views.PageView', endpoint='flatpage_cls') views.add_admin('admin.AdminView', endpoint='app_admin', name='App View') views.add_error(404, 'views.error') views.add_error(500, 'views.error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}, endpoint='favicon') # Register test blueprint app.register_blueprint(test_blueprint, url_prefix='/test')
from flask.ext.flatpages import FlatPages from flask.ext.lazyviews import LazyViews from flask.ext.mail import Mail from flask.ext.script import Manager from learnpython import settings # Initialize Flask application app = Flask('learnpython') app.config.from_object(settings) # Configure all necessary plugins babel = Babel(app) mail = Mail(app) manager = Manager(app) pages = FlatPages(app) # Register all possible urls views = LazyViews(app, '.views') views.add('/', 'page', defaults={'name': 'index'}, endpoint='index') views.add('/contacts', 'contacts', methods=('GET', 'POST')) views.add('/flows', 'flows', endpoint='flows') views.add('/flows/archive/<int:archive>', 'flows', endpoint='flows_archive') views.add('/status', 'status') views.add('/subscribe', 'subscribe', methods=('GET', 'POST')) views.add_error(404, 'error') views.add_error(500, 'error') views.add_static('/favicon.ico', defaults={'filename': 'img/favicon.ico'}) views.add('/<path:name>', 'page')