Skip to content
Myr

More modules -> blueprints

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 7e55b50 · Browse files · 2 files+8 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+8 added−8 removed

  • other:+8 added−8 removed

2 files+8 added−8 removed

modified:docs/patterns/appfactories.rst+7 added-7 removed
4 unmodified lines
56If you are already using packages and blueprints for your application7(:ref:`blueprints`) there are a couple of really nice ways to further improve8the experience. A common pattern is creating the application object when9the module is imported. But if you move the creation of this object,9the blueprint is imported. But if you move the creation of this object,10into a function, you can then create multiple instances of this and later.1112So why would you want to do this?13
17 unmodified lines
31 app.config.from_pyfile(config_filename)3233 from yourapplication.views.admin import admin34 from yourapplication.views.frontend import frontend35 app.register_module(admin)36 app.register_module(frontend)35 app.register_blueprint(admin)36 app.register_blueprint(frontend)3738 return app3940The downside is that you cannot use the application object in the modules40The downside is that you cannot use the application object in the blueprints41at import time. You can however use it from within a request. How do you42get access the application with the config? Use43:data:`~flask.current_app`::4445 from flask import current_app, Module, render_template46 admin = Module(__name__, url_prefix='/admin')45 from flask import current_app, Blueprint, render_template46 admin = Blueprint('admin', __name__, url_prefix='/admin')4748 @admin.route('/')49 def index():50 return render_template(current_app.config['INDEX_TEMPLATE'])
17 unmodified lines
68it. The following changes are straightforward and possible:69701. make it possible to pass in configuration values for unittests so that71 you don't have to create config files on the filesystem722. call a function from a module when the application is setting up so722. call a function from a blueprint when the application is setting up so73 that you have a place to modify attributes of the application (like74 hooking in before / after request handlers etc.)753. Add in WSGI middlewares when the application is creating if necessary.
modified:docs/config.rst+1 added-1 removed
166 unmodified lines
167a little harder. There is no one 100% solution for this problem in168general, but there are a couple of things you can do to improve that169experience:1701711. create your application in a function and register modules on it.1711. create your application in a function and register blueprints on it.172 That way you can create multiple instances of your application with173 different configurations attached which makes unittesting a lot174 easier. You can use this to pass in configuration as needed.175
72 unmodified lines