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
Provenance
- Commit
d38c61f- Tree
be62c2c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7e55b50
2 files changed+8 added−8 removed
modified:docs/patterns/appfactories.rst+7 added-7 removed
4 unmodified lines5 6 If you are already using packages and blueprints for your application7 (:ref:`blueprints`) there are a couple of really nice ways to further improve8 the experience. A common pattern is creating the application object when9-the module is imported. But if you move the creation of this object,9+the blueprint is imported. But if you move the creation of this object,10 into a function, you can then create multiple instances of this and later.11 12 So why would you want to do this?13 17 unmodified lines31 app.config.from_pyfile(config_filename)32 33 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)37 38 return app39 40-The downside is that you cannot use the application object in the modules40+The downside is that you cannot use the application object in the blueprints41 at import time. You can however use it from within a request. How do you42 get access the application with the config? Use43 :data:`~flask.current_app`::44 45- 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')47 48 @admin.route('/')49 def index():50 return render_template(current_app.config['INDEX_TEMPLATE'])17 unmodified lines68 it. The following changes are straightforward and possible:69 70 1. make it possible to pass in configuration values for unittests so that71 you don't have to create config files on the filesystem72-2. call a function from a module when the application is setting up so72+2. 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.)75 3. Add in WSGI middlewares when the application is creating if necessary.modified:docs/config.rst+1 added-1 removed
166 unmodified lines167 a little harder. There is no one 100% solution for this problem in168 general, but there are a couple of things you can do to improve that169 experience:170 171-1. create your application in a function and register modules on it.171+1. 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