Skip to content
Myr

Deduplicate signals docs

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

Triggered by #1390

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parent 3e07b9c · Browse files · 2 files+109 added−181 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+109 added−181 removed

  • other:+109 added−181 removed

2 files+109 added−181 removed

modified:docs/api.rst+108 added-18 removed
506 unmodified lines
507508.. autoclass:: flask.blueprints.BlueprintSetupState509 :members:510511.. _core-signals-list:512513Signals514-------515514.. when modifying this list, also update the one in signals.rst515516.. versionadded:: 0.6517518.. data:: signals_available518.. data:: signals.signals_available519520 ``True`` if the signaling system is available. This is the case521 when `blinker`_ is installed.522523The following signals exist in Flask:524525.. data:: template_rendered526527 This signal is sent when a template was successfully rendered. The528 signal is invoked with the instance of the template as `template`529 and the context as dictionary (named `context`).530531 Example subscriber::532533 def log_template_renders(sender, template, context, **extra):534 sender.logger.debug('Rendering template "%s" with context %s',535 template.name or 'string template',536 context)537538 from flask import template_rendered539 template_rendered.connect(log_template_renders, app)540541.. data:: request_started542531 This signal is sent before any request processing started but when the532 request context was set up. Because the request context is already543 This signal is sent when the request context is set up, before544 any request processing happens. Because the request context is already545 bound, the subscriber can access the request with the standard global546 proxies such as :class:`~flask.request`.547548 Example subscriber::549550 def log_request(sender, **extra):551 sender.logger.debug('Request context is set up')552553 from flask import request_started554 request_started.connect(log_request, app)555556.. data:: request_finished557558 This signal is sent right before the response is sent to the client.559 It is passed the response to be sent named `response`.560561 Example subscriber::562563 def log_response(sender, response, **extra):564 sender.logger.debug('Request context is about to close down. '565 'Response: %s', response)566567 from flask import request_finished568 request_finished.connect(log_response, app)569570.. data:: got_request_exception571572 This signal is sent when an exception happens during request processing.573 It is sent *before* the standard exception handling kicks in and even574 in debug mode, where no exception handling happens. The exception575 itself is passed to the subscriber as `exception`.576577 Example subscriber::578579 def log_exception(sender, exception, **extra):580 sender.logger.debug('Got exception during processing: %s', exception)581582 from flask import got_request_exception583 got_request_exception.connect(log_exception, app)584585.. data:: request_tearing_down586550 This signal is sent when the application is tearing down the request.551 This is always called, even if an error happened. An `exc` keyword552 argument is passed with the exception that caused the teardown.587 This signal is sent when the request is tearing down. This is always588 called, even if an exception is caused. Currently functions listening589 to this signal are called after the regular teardown handlers, but this590 is not something you can rely on.591554 .. versionchanged:: 0.9555 The `exc` parameter was added.592 Example subscriber::593594 def close_db_connection(sender, **extra):595 session.close()596597 from flask import request_tearing_down598 request_tearing_down.connect(close_db_connection, app)599600 As of Flask 0.9, this will also be passed an `exc` keyword argument601 that has a reference to the exception that caused the teardown if602 there was one.603604.. data:: appcontext_tearing_down605559 This signal is sent when the application is tearing down the560 application context. This is always called, even if an error happened.561 An `exc` keyword argument is passed with the exception that caused the562 teardown. The sender is the application.606 This signal is sent when the app context is tearing down. This is always607 called, even if an exception is caused. Currently functions listening608 to this signal are called after the regular teardown handlers, but this609 is not something you can rely on.610611 Example subscriber::612613 def close_db_connection(sender, **extra):614 session.close()615616 from flask import appcontext_tearing_down617 appcontext_tearing_down.connect(close_db_connection, app)618619 This will also be passed an `exc` keyword argument that has a reference620 to the exception that caused the teardown if there was one.621622.. data:: appcontext_pushed623624 This signal is sent when an application context is pushed. The sender567 is the application.625 is the application. This is usually useful for unittests in order to626 temporarily hook in information. For instance it can be used to627 set a resource early onto the `g` object.628629 Example usage::630631 from contextlib import contextmanager632 from flask import appcontext_pushed633634 @contextmanager635 def user_set(app, user):636 def handler(sender, **kwargs):637 g.user = user638 with appcontext_pushed.connected_to(handler, app):639 yield640641 And in the testcode::642643 def test_user_me(self):644 with user_set(app, 'john'):645 c = app.test_client()646 resp = c.get('/users/me')647 assert resp.data == 'username=john'648649 .. versionadded:: 0.10650651.. data:: appcontext_popped652
2 unmodified lines
655 :data:`appcontext_tearing_down` signal.656657 .. versionadded:: 0.10658659660.. data:: message_flashed661662 This signal is sent when the application is flashing a message. The663 messages is sent as `message` keyword argument and the category as664 `category`.665585 .. versionadded:: 0.10666 Example subscriber::667587.. currentmodule:: None668 recorded = []669 def record(sender, message, category, **extra):670 recorded.append((message, category))671589.. class:: flask.signals.Namespace672 from flask import message_flashed673 message_flashed.connect(record, app)674675 .. versionadded:: 0.10676677.. class:: signals.Namespace678679 An alias for :class:`blinker.base.Namespace` if blinker is available,680 otherwise a dummy class that creates fake signals. This class is681 available for Flask extensions that want to provide the same fallback682 system as Flask itself.
4 unmodified lines
687 otherwise returns a fake signal that has a send method that will688 do nothing but will fail with a :exc:`RuntimeError` for all other689 operations, including connecting.690691692.. _blinker: https://pypi.python.org/pypi/blinker693694695Class-Based Views696-----------------697698.. versionadded:: 0.7
184 unmodified lines
modified:docs/signals.rst+1 added-163 removed
182 unmodified lines
183184Core Signals185------------186187.. when modifying this list, also update the one in api.rst187Take a look at :ref:`core-signals-list` for a list of all builtin signals.188189The following signals exist in Flask:189191.. data:: flask.template_rendered192 :noindex:193194 This signal is sent when a template was successfully rendered. The195 signal is invoked with the instance of the template as `template`196 and the context as dictionary (named `context`).197198 Example subscriber::199200 def log_template_renders(sender, template, context, **extra):201 sender.logger.debug('Rendering template "%s" with context %s',202 template.name or 'string template',203 context)204205 from flask import template_rendered206 template_rendered.connect(log_template_renders, app)207208.. data:: flask.request_started209 :noindex:210211 This signal is sent when the request context is set up, before212 any request processing happens. Because the request context is already213 bound, the subscriber can access the request with the standard global214 proxies such as :class:`~flask.request`.215216 Example subscriber::217218 def log_request(sender, **extra):219 sender.logger.debug('Request context is set up')220221 from flask import request_started222 request_started.connect(log_request, app)223224.. data:: flask.request_finished225 :noindex:226227 This signal is sent right before the response is sent to the client.228 It is passed the response to be sent named `response`.229230 Example subscriber::231232 def log_response(sender, response, **extra):233 sender.logger.debug('Request context is about to close down. '234 'Response: %s', response)235236 from flask import request_finished237 request_finished.connect(log_response, app)238239.. data:: flask.got_request_exception240 :noindex:241242 This signal is sent when an exception happens during request processing.243 It is sent *before* the standard exception handling kicks in and even244 in debug mode, where no exception handling happens. The exception245 itself is passed to the subscriber as `exception`.246247 Example subscriber::248249 def log_exception(sender, exception, **extra):250 sender.logger.debug('Got exception during processing: %s', exception)251252 from flask import got_request_exception253 got_request_exception.connect(log_exception, app)254255.. data:: flask.request_tearing_down256 :noindex:257258 This signal is sent when the request is tearing down. This is always259 called, even if an exception is caused. Currently functions listening260 to this signal are called after the regular teardown handlers, but this261 is not something you can rely on.262263 Example subscriber::264265 def close_db_connection(sender, **extra):266 session.close()267268 from flask import request_tearing_down269 request_tearing_down.connect(close_db_connection, app)270271 As of Flask 0.9, this will also be passed an `exc` keyword argument272 that has a reference to the exception that caused the teardown if273 there was one.274275.. data:: flask.appcontext_tearing_down276 :noindex:277278 This signal is sent when the app context is tearing down. This is always279 called, even if an exception is caused. Currently functions listening280 to this signal are called after the regular teardown handlers, but this281 is not something you can rely on.282283 Example subscriber::284285 def close_db_connection(sender, **extra):286 session.close()287288 from flask import appcontext_tearing_down289 appcontext_tearing_down.connect(close_db_connection, app)290291 This will also be passed an `exc` keyword argument that has a reference292 to the exception that caused the teardown if there was one.293294.. data:: flask.appcontext_pushed295 :noindex:296297 This signal is sent when an application context is pushed. The sender298 is the application. This is usually useful for unittests in order to299 temporarily hook in information. For instance it can be used to300 set a resource early onto the `g` object.301302 Example usage::303304 from contextlib import contextmanager305 from flask import appcontext_pushed306307 @contextmanager308 def user_set(app, user):309 def handler(sender, **kwargs):310 g.user = user311 with appcontext_pushed.connected_to(handler, app):312 yield313314 And in the testcode::315316 def test_user_me(self):317 with user_set(app, 'john'):318 c = app.test_client()319 resp = c.get('/users/me')320 assert resp.data == 'username=john'321322 .. versionadded:: 0.10323324.. data:: flask.appcontext_popped325 :noindex:326327 This signal is sent when an application context is popped. The sender328 is the application. This usually falls in line with the329 :data:`appcontext_tearing_down` signal.330331 .. versionadded:: 0.10332333334.. data:: flask.message_flashed335 :noindex:336337 This signal is sent when the application is flashing a message. The338 messages is sent as `message` keyword argument and the category as339 `category`.340341 Example subscriber::342343 recorded = []344 def record(sender, message, category, **extra):345 recorded.append((message, category))346347 from flask import message_flashed348 message_flashed.connect(record, app)349350 .. versionadded:: 0.10351190.. _blinker: https://pypi.python.org/pypi/blinker