Skip to content
Myr

Merge branch 'blueprints' of github.com:mitsuhiko/flask into 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 · parents 0fcbe0a, 3d6e3e4 · Browse files · 3 files+60 added−50 removed
This commit

State

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

Diff stats

3 files+60 added−50 removed

  • other:+60 added−50 removed

3 files+60 added−50 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
146 unmodified lines
modified:docs/reqcontext.rst+5 added-4 removed
172 unmodified lines
173It's easy to see the behavior from the command line:174175>>> app = Flask(__name__)176>>> @app.teardown_request177... def after_request(exception=None):178... print 'after request'179... 177... def teardown_request(exception=None):178... print 'this runs after request'179...180>>> ctx = app.test_request_context()181>>> ctx.push()182>>> ctx.pop()183after request183this runs after request184>>>185186.. _notes-on-proxies:187188Notes On Proxies
43 unmodified lines
modified:flask/blueprints.py+20 added-20 removed
122 unmodified lines
123 for deferred in self.deferred_functions:124 deferred(state)125126 def route(self, rule, **options):127 """Like :meth:`Flask.route` but for a module. The endpoint for the128 :func:`url_for` function is prefixed with the name of the module.127 """Like :meth:`Flask.route` but for a blueprint. The endpoint for the128 :func:`url_for` function is prefixed with the name of the blueprint.129 """130 def decorator(f):131 self.add_url_rule(rule, f.__name__, f, **options)132 return f133 return decorator134135 def add_url_rule(self, rule, endpoint=None, view_func=None, **options):136 """Like :meth:`Flask.add_url_rule` but for a module. The endpoint for137 the :func:`url_for` function is prefixed with the name of the module.136 """Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for137 the :func:`url_for` function is prefixed with the name of the blueprint.138 """139 self.record(lambda s:140 s.add_url_rule(rule, endpoint, view_func, **options))141142 def endpoint(self, endpoint):143 """Like :meth:`Flask.endpoint` but for a module. This does not144 prefix the endpoint with the module name, this has to be done143 """Like :meth:`Flask.endpoint` but for a blueprint. This does not144 prefix the endpoint with the blueprint name, this has to be done145 explicitly by the user of this method. If the endpoint is prefixed146 with a `.` it will be registered to the current blueprint, otherwise147 it's an application independent endpoint.148 """
4 unmodified lines
153 return f154 return decorator155156 def before_request(self, f):157 """Like :meth:`Flask.before_request` but for a module. This function157 """Like :meth:`Flask.before_request` but for a blueprint. This function158 is only executed before each request that is handled by a function of159 that module.159 that blueprint.160 """161 self.record_once(lambda s: s.app.before_request_funcs162 .setdefault(self.name, []).append(f))163 return f164165 def before_app_request(self, f):166 """Like :meth:`Flask.before_request`. Such a function is executed167 before each request, even if outside of a module.167 before each request, even if outside of a blueprint.168 """169 self.record_once(lambda s: s.app.before_request_funcs170 .setdefault(None, []).append(f))171 return f172173 def after_request(self, f):174 """Like :meth:`Flask.after_request` but for a module. This function174 """Like :meth:`Flask.after_request` but for a blueprint. This function175 is only executed after each request that is handled by a function of176 that module.176 that blueprint.177 """178 self.record_once(lambda s: s.app.after_request_funcs179 .setdefault(self.name, []).append(f))180 return f181182 def after_app_request(self, f):183 """Like :meth:`Flask.after_request` but for a module. Such a function184 is executed after each request, even if outside of the module.183 """Like :meth:`Flask.after_request` but for a blueprint. Such a function184 is executed after each request, even if outside of the blueprint.185 """186 self.record_once(lambda s: s.app.after_request_funcs187 .setdefault(None, []).append(f))188 return f189190 def context_processor(self, f):191 """Like :meth:`Flask.context_processor` but for a module. This192 function is only executed for requests handled by a module.191 """Like :meth:`Flask.context_processor` but for a blueprint. This192 function is only executed for requests handled by a blueprint.193 """194 self.record_once(lambda s: s.app.template_context_processors195 .setdefault(self.name, []).append(f))196 return f197198 def app_context_processor(self, f):199 """Like :meth:`Flask.context_processor` but for a module. Such a200 function is executed each request, even if outside of the module.199 """Like :meth:`Flask.context_processor` but for a blueprint. Such a200 function is executed each request, even if outside of the blueprint.201 """202 self.record_once(lambda s: s.app.template_context_processors203 .setdefault(None, []).append(f))204 return f205206 def app_errorhandler(self, code):207 """Like :meth:`Flask.errorhandler` but for a module. This208 handler is used for all requests, even if outside of the module.207 """Like :meth:`Flask.errorhandler` but for a blueprint. This208 handler is used for all requests, even if outside of the blueprint.209 """210 def decorator(f):211 self.record_once(lambda s: s.app.errorhandler(code)(f))212 return f
8 unmodified lines
221 .setdefault(self.name, []).append(f))222 return f223224 def url_defaults(self, f):225 """Callback function for URL defaults for this module. It's called225 """Callback function for URL defaults for this blueprint. It's called226 with the endpoint and values and should update the values passed227 in place.228 """229 self.record_once(lambda s: s.app.url_default_functions
35 unmodified lines