Skip to content
Myr

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

1 file+35 added−26 removed

modified:docs/blueprints.rst+35 added-26 removed
3 unmodified lines
4====================================56.. versionadded:: 0.778Flask knows a concept known as “blueprints” which can greatly simplify how9large applications work. A blueprint is an object works similar to an10actual :class:`Flask` application object, but it is not actually an11application. Rather it is the blueprint of how to create an application.12Think of it like that: you might want to have an application that has a13wiki. So what you can do is creating the blueprint for a wiki and then14let the application assemble the wiki on the application object.8Flask uses a concept of *blueprints* for making application components and9supporting common patterns within an application or across applications.10Blueprints can greatly simplify how large applications work and provide a11central means for Flask extensions to register operations on applications.12A :class:`Blueprint` object works similarly to a :class:`Flask`13application object, but it is not actually an application. Rather it is a14*blueprint* of how to construct or extend an application.1516Why Blueprints?17---------------1819Why have blueprints and not multiple application objects? The utopia of20pluggable applications are different WSGI applications and merging them21together somehow. You can do that (see :ref:`app-dispatch`) but it's not22the right tool for every case. Having different applications means having23different configs. Applications are also separated on the WSGI layer24which is a lot lower level than the level that Flask usually operates on25where you have request and response objects.19Blueprints in Flask are intended for these cases:2027Blueprints do not necessarily have to implement applications. They could28only provide filters for templates, static files, templates or similar29things. They share the same config as the application and can change the30application 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.3432The downside is that you cannot unregister a blueprint once application33without having to destroy the whole application object.35A blueprint in Flask is not a pluggable app because it is not actually an36application -- it's a set of operations which can be registered on an37application, even multiple times. Why not have multiple application38objects? You can do that (see :ref:`app-dispatch`), but your applications39will have separate configs and will be managed at the WSGI layer.4041Blueprints instead provide separation at the Flask level, share42application config, and can change an application object as necessary with43being registered. The downside is that you cannot unregister a blueprint44once application without having to destroy the whole application object.4546The Concept of Blueprints47-------------------------4838The basic concept of blueprints is that they record operations that should39be executed when the blueprint is registered on the application. However40additionally each time a request gets dispatched to a view that was41declared to a blueprint Flask will remember that the request was42dispatched to that blueprint. That way it's easier to generate URLs from43one endpoint to another in the same module.49The basic concept of blueprints is that they record operations to execute50when registered on an application. Flask associates view functions with51blueprints when dispatching requests and generating URLs from one endpoint52to another.5354My First Blueprint55------------------56
15 unmodified lines