Expand documentation on blueprints.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ron DuPlain · committed by Ron DuPlain · parent 20bf8ed · Browse files · 1 file·+35 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+35 added−26 removed
- other:+35 added−26 removed
Provenance
- Commit
0313c8b- Tree
80f08c2- Author
Ron DuPlain <ron.duplain@gmail.com>- Committer
Ron DuPlain <ron.duplain@gmail.com>- Parent
20bf8ed
1 file changed+35 added−26 removed
modified:docs/blueprints.rst+35 added-26 removed
3 unmodified lines4 ====================================5 6 .. versionadded:: 0.77 8-Flask knows a concept known as “blueprints” which can greatly simplify how9-large applications work. A blueprint is an object works similar to an10-actual :class:`Flask` application object, but it is not actually an11-application. Rather it is the blueprint of how to create an application.12-Think of it like that: you might want to have an application that has a13-wiki. So what you can do is creating the blueprint for a wiki and then14-let the application assemble the wiki on the application object.8+Flask uses a concept of *blueprints* for making application components and9+supporting common patterns within an application or across applications.10+Blueprints can greatly simplify how large applications work and provide a11+central means for Flask extensions to register operations on applications.12+A :class:`Blueprint` object works similarly to a :class:`Flask`13+application object, but it is not actually an application. Rather it is a14+*blueprint* of how to construct or extend an application.15 16 Why Blueprints?17 ---------------18 19-Why have blueprints and not multiple application objects? The utopia of20-pluggable applications are different WSGI applications and merging them21-together somehow. You can do that (see :ref:`app-dispatch`) but it's not22-the right tool for every case. Having different applications means having23-different configs. Applications are also separated on the WSGI layer24-which is a lot lower level than the level that Flask usually operates on25-where you have request and response objects.19+Blueprints in Flask are intended for these cases:20 27-Blueprints do not necessarily have to implement applications. They could28-only provide filters for templates, static files, templates or similar29-things. They share the same config as the application and can change the30-application as necessary when being registered.21+* Factor an application into a set of blueprints. This is ideal for22+ larger applications; a project could instantiate an application object,23+ initialize several extensions, and register a collection of blueprints.24+* Register a blueprint on an application at a URL prefix and/or subdomain.25+ Paremeters in the URL prefix/subdomain become common view arguments26+ (with defaults) across all view functions in the blueprint.27+* Register a blueprint multiple times on an application with different URL28+ rules.29+* Provide template filters, static files, templates, and other utilities30+ through blueprints. A blueprint does not have to implement applications31+ or view functions.32+* Register a blueprint on an application for any of these cases when33+ initializing a Flask extension.34 32-The downside is that you cannot unregister a blueprint once application33-without having to destroy the whole application object.35+A blueprint in Flask is not a pluggable app because it is not actually an36+application -- it's a set of operations which can be registered on an37+application, even multiple times. Why not have multiple application38+objects? You can do that (see :ref:`app-dispatch`), but your applications39+will have separate configs and will be managed at the WSGI layer.40 41+Blueprints instead provide separation at the Flask level, share42+application config, and can change an application object as necessary with43+being registered. The downside is that you cannot unregister a blueprint44+once application without having to destroy the whole application object.45+46 The Concept of Blueprints47 -------------------------48 38-The basic concept of blueprints is that they record operations that should39-be executed when the blueprint is registered on the application. However40-additionally each time a request gets dispatched to a view that was41-declared to a blueprint Flask will remember that the request was42-dispatched to that blueprint. That way it's easier to generate URLs from43-one endpoint to another in the same module.49+The basic concept of blueprints is that they record operations to execute50+when registered on an application. Flask associates view functions with51+blueprints when dispatching requests and generating URLs from one endpoint52+to another.53 54 My First Blueprint55 ------------------56 15 unmodified lines