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
Provenance
- Commit
5ca333e- Tree
863344c- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
0fcbe0a- Parent 2
3d6e3e4
3 files changed+60 added−50 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 146 unmodified linesmodified:docs/reqcontext.rst+5 added-4 removed
172 unmodified lines173 It's easy to see the behavior from the command line:174 175 >>> 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()183-after request183+this runs after request184+>>>185 186 .. _notes-on-proxies:187 188 Notes On Proxies43 unmodified linesmodified:flask/blueprints.py+20 added-20 removed
122 unmodified lines123 for deferred in self.deferred_functions:124 deferred(state)125 126 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 decorator134 135 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))141 142 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 lines153 return f154 return decorator155 156 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 f164 165 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 f172 173 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 f181 182 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 f189 190 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 f197 198 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 f205 206 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 f8 unmodified lines221 .setdefault(self.name, []).append(f))222 return f223 224 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_functions35 unmodified lines