Merge pull request #4971 from pallets/docs-blueprint-app-methods
Before adoption: this commit came in with the history the project adopted, so no landing records it.
point to app-scoped blueprint methods
authored by David Lord · committed by GitHub · parents d552726, ab93222 · Browse files · 2 files·+77 added−33 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+77 added−33 removed
- other:+77 added−33 removed
Provenance
- Commit
129568f- Tree
5f6017f- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d552726- Parent 2
ab93222
2 files changed+77 added−33 removed
modified:src/flask/blueprints.py+38 added-32 removed
476 unmodified lines477 view_func: t.Optional[ft.RouteCallable] = None,478 provide_automatic_options: t.Optional[bool] = None,479 **options: t.Any,480 ) -> None:481- """Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for482- the :func:`url_for` function is prefixed with the name of the blueprint.481+ """Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for482+ full documentation.483+484+ The URL rule is prefixed with the blueprint's URL prefix. The endpoint name,485+ used with :func:`url_for`, is prefixed with the blueprint's name.486 """487 if endpoint and "." in endpoint:488 raise ValueError("'endpoint' may not contain a dot '.' character.")489 13 unmodified lines503 @setupmethod504 def app_template_filter(505 self, name: t.Optional[str] = None506 ) -> t.Callable[[T_template_filter], T_template_filter]:504- """Register a custom template filter, available application wide. Like505- :meth:`Flask.template_filter` but for a blueprint.507+ """Register a template filter, available in any template rendered by the508+ application. Equivalent to :meth:`.Flask.template_filter`.509 510 :param name: the optional name of the filter, otherwise the511 function name will be used.512 """7 unmodified lines520 @setupmethod521 def add_app_template_filter(522 self, f: ft.TemplateFilterCallable, name: t.Optional[str] = None523 ) -> None:521- """Register a custom template filter, available application wide. Like522- :meth:`Flask.add_template_filter` but for a blueprint. Works exactly523- like the :meth:`app_template_filter` decorator.524+ """Register a template filter, available in any template rendered by the525+ application. Works like the :meth:`app_template_filter` decorator. Equivalent to526+ :meth:`.Flask.add_template_filter`.527 528 :param name: the optional name of the filter, otherwise the529 function name will be used.530 """6 unmodified lines537 @setupmethod538 def app_template_test(539 self, name: t.Optional[str] = None540 ) -> t.Callable[[T_template_test], T_template_test]:538- """Register a custom template test, available application wide. Like539- :meth:`Flask.template_test` but for a blueprint.541+ """Register a template test, available in any template rendered by the542+ application. Equivalent to :meth:`.Flask.template_test`.543 544 .. versionadded:: 0.10545 546 :param name: the optional name of the test, otherwise the9 unmodified lines556 @setupmethod557 def add_app_template_test(558 self, f: ft.TemplateTestCallable, name: t.Optional[str] = None559 ) -> None:557- """Register a custom template test, available application wide. Like558- :meth:`Flask.add_template_test` but for a blueprint. Works exactly559- like the :meth:`app_template_test` decorator.560+ """Register a template test, available in any template rendered by the561+ application. Works like the :meth:`app_template_test` decorator. Equivalent to562+ :meth:`.Flask.add_template_test`.563 564 .. versionadded:: 0.10565 566 :param name: the optional name of the test, otherwise the8 unmodified lines575 @setupmethod576 def app_template_global(577 self, name: t.Optional[str] = None578 ) -> t.Callable[[T_template_global], T_template_global]:576- """Register a custom template global, available application wide. Like577- :meth:`Flask.template_global` but for a blueprint.579+ """Register a template global, available in any template rendered by the580+ application. Equivalent to :meth:`.Flask.template_global`.581 582 .. versionadded:: 0.10583 584 :param name: the optional name of the global, otherwise the9 unmodified lines594 @setupmethod595 def add_app_template_global(596 self, f: ft.TemplateGlobalCallable, name: t.Optional[str] = None597 ) -> None:595- """Register a custom template global, available application wide. Like596- :meth:`Flask.add_template_global` but for a blueprint. Works exactly597- like the :meth:`app_template_global` decorator.598+ """Register a template global, available in any template rendered by the599+ application. Works like the :meth:`app_template_global` decorator. Equivalent to600+ :meth:`.Flask.add_template_global`.601 602 .. versionadded:: 0.10603 604 :param name: the optional name of the global, otherwise the6 unmodified lines611 self.record_once(register_template)612 613 @setupmethod614 def before_app_request(self, f: T_before_request) -> T_before_request:612- """Like :meth:`Flask.before_request`. Such a function is executed613- before each request, even if outside of a blueprint.615+ """Like :meth:`before_request`, but before every request, not only those handled616+ by the blueprint. Equivalent to :meth:`.Flask.before_request`.617 """618 self.record_once(619 lambda s: s.app.before_request_funcs.setdefault(None, []).append(f)620 )2 unmodified lines623 @setupmethod624 def before_app_first_request(625 self, f: T_before_first_request626 ) -> T_before_first_request:624- """Like :meth:`Flask.before_first_request`. Such a function is625- executed before the first request to the application.627+ """Register a function to run before the first request to the application is628+ handled by the worker. Equivalent to :meth:`.Flask.before_first_request`.629 630 .. deprecated:: 2.2631 Will be removed in Flask 2.3. Run setup code when creating632 the application instead.11 unmodified lines644 return f645 646 @setupmethod647 def after_app_request(self, f: T_after_request) -> T_after_request:645- """Like :meth:`Flask.after_request` but for a blueprint. Such a function646- is executed after each request, even if outside of the blueprint.648+ """Like :meth:`after_request`, but after every request, not only those handled649+ by the blueprint. Equivalent to :meth:`.Flask.after_request`.650 """651 self.record_once(652 lambda s: s.app.after_request_funcs.setdefault(None, []).append(f)653 )654 return f655 656 @setupmethod657 def teardown_app_request(self, f: T_teardown) -> T_teardown:655- """Like :meth:`Flask.teardown_request` but for a blueprint. Such a656- function is executed when tearing down each request, even if outside of657- the blueprint.658+ """Like :meth:`teardown_request`, but after every request, not only those659+ handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`.660 """661 self.record_once(662 lambda s: s.app.teardown_request_funcs.setdefault(None, []).append(f)663 )2 unmodified lines666 @setupmethod667 def app_context_processor(668 self, f: T_template_context_processor669 ) -> T_template_context_processor:668- """Like :meth:`Flask.context_processor` but for a blueprint. Such a669- function is executed each request, even if outside of the blueprint.670+ """Like :meth:`context_processor`, but for templates rendered by every view, not671+ only by the blueprint. Equivalent to :meth:`.Flask.context_processor`.672 """673 self.record_once(674 lambda s: s.app.template_context_processors.setdefault(None, []).append(f)675 )2 unmodified lines678 @setupmethod679 def app_errorhandler(680 self, code: t.Union[t.Type[Exception], int]681 ) -> t.Callable[[T_error_handler], T_error_handler]:680- """Like :meth:`Flask.errorhandler` but for a blueprint. This681- handler is used for all requests, even if outside of the blueprint.682+ """Like :meth:`errorhandler`, but for every request, not only those handled by683+ the blueprint. Equivalent to :meth:`.Flask.errorhandler`.684 """685 686 def decorator(f: T_error_handler) -> T_error_handler:687 self.record_once(lambda s: s.app.errorhandler(code)(f))4 unmodified lines692 @setupmethod693 def app_url_value_preprocessor(694 self, f: T_url_value_preprocessor695 ) -> T_url_value_preprocessor:694- """Same as :meth:`url_value_preprocessor` but application wide."""696+ """Like :meth:`url_value_preprocessor`, but for every request, not only those697+ handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`.698+ """699 self.record_once(700 lambda s: s.app.url_value_preprocessors.setdefault(None, []).append(f)701 )702 return f703 704 @setupmethod705 def app_url_defaults(self, f: T_url_defaults) -> T_url_defaults:702- """Same as :meth:`url_defaults` but application wide."""706+ """Like :meth:`url_defaults`, but for every request, not only those handled by707+ the blueprint. Equivalent to :meth:`.Flask.url_defaults`.708+ """709 self.record_once(710 lambda s: s.app.url_default_functions.setdefault(None, []).append(f)711 )712 return fmodified:src/flask/scaffold.py+39 added-1 removed
559 unmodified lines560 The function will be called without any arguments. If it returns561 a non-``None`` value, the value is handled as if it was the562 return value from the view, and further request handling is563 stopped.564+565+ This is available on both app and blueprint objects. When used on an app, this566+ executes before every request. When used on a blueprint, this executes before567+ every request that the blueprint handles. To register with a blueprint and568+ execute before every request, use :meth:`.Blueprint.before_app_request`.569 """570 self.before_request_funcs.setdefault(None, []).append(f)571 return f572 8 unmodified lines581 If a function raises an exception, any remaining582 ``after_request`` functions will not be called. Therefore, this583 should not be used for actions that must execute, such as to584 close resources. Use :meth:`teardown_request` for that.585+586+ This is available on both app and blueprint objects. When used on an app, this587+ executes after every request. When used on a blueprint, this executes after588+ every request that the blueprint handles. To register with a blueprint and589+ execute after every request, use :meth:`.Blueprint.after_app_request`.590 """591 self.after_request_funcs.setdefault(None, []).append(f)592 return f593 21 unmodified lines615 execute code that might fail they must surround that code with a616 ``try``/``except`` block and log any errors.617 618 The return values of teardown functions are ignored.619+620+ This is available on both app and blueprint objects. When used on an app, this621+ executes after every request. When used on a blueprint, this executes after622+ every request that the blueprint handles. To register with a blueprint and623+ execute after every request, use :meth:`.Blueprint.teardown_app_request`.624 """625 self.teardown_request_funcs.setdefault(None, []).append(f)626 return f627 1 unmodified line629 def context_processor(630 self,631 f: T_template_context_processor,632 ) -> T_template_context_processor:618- """Registers a template context processor function."""633+ """Registers a template context processor function. These functions run before634+ rendering a template. The keys of the returned dict are added as variables635+ available in the template.636+637+ This is available on both app and blueprint objects. When used on an app, this638+ is called for every rendered template. When used on a blueprint, this is called639+ for templates rendered from the blueprint's views. To register with a blueprint640+ and affect every template, use :meth:`.Blueprint.app_context_processor`.641+ """642 self.template_context_processors[None].append(f)643 return f644 645 @setupmethod11 unmodified lines657 every view.658 659 The function is passed the endpoint name and values dict. The return660 value is ignored.661+662+ This is available on both app and blueprint objects. When used on an app, this663+ is called for every request. When used on a blueprint, this is called for664+ requests that the blueprint handles. To register with a blueprint and affect665+ every request, use :meth:`.Blueprint.app_url_value_preprocessor`.666 """667 self.url_value_preprocessors[None].append(f)668 return f669 1 unmodified line671 def url_defaults(self, f: T_url_defaults) -> T_url_defaults:672 """Callback function for URL defaults for all view functions of the673 application. It's called with the endpoint and values and should674 update the values passed in place.675+676+ This is available on both app and blueprint objects. When used on an app, this677+ is called for every request. When used on a blueprint, this is called for678+ requests that the blueprint handles. To register with a blueprint and affect679+ every request, use :meth:`.Blueprint.app_url_defaults`.680 """681 self.url_default_functions[None].append(f)682 return f683 15 unmodified lines699 @app.errorhandler(DatabaseError)700 def special_exception_handler(error):701 return 'Database connection failed', 500702 703+ This is available on both app and blueprint objects. When used on an app, this704+ can handle errors from every request. When used on a blueprint, this can handle705+ errors from requests that the blueprint handles. To register with a blueprint706+ and affect every request, use :meth:`.Blueprint.app_errorhandler`.707+708 .. versionadded:: 0.7709 Use :meth:`register_error_handler` instead of modifying710 :attr:`error_handler_spec` directly, for application wide error711 handlers.225 unmodified lines