appcontext.rst
This file
Path
docs/appcontext.rst
159 lines · 5.4 KiB
Last changed by
Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
History of this fileTake it
The walk stopped after 200 commits: lines older than d2a46dc point at it.
Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 1–2why, one step back1.. currentmodule:: flask2fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 3–7why, one step back3.. _app-context:45The Application Context6=======================7Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 8–11why, one step back8The application context keeps track of the application-level data during9a request, CLI command, or other activity. Rather than passing the10application around to each function, the :data:`current_app` and11:data:`g` proxies are accessed instead.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 12why, one step back12Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 13–15why, one step back13This is similar to the :doc:`/reqcontext`, which keeps track of14request-level data during a request. A corresponding application context15is pushed when a request context is pushed.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 16why, one step back16Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 17–18why, one step back17Purpose of the Context18----------------------fix doc build errorDavid Lordbefore adoptiond2a46dc
line 19why, one step back19Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 20–27why, one step back20The :class:`Flask` application object has attributes, such as21:attr:`~Flask.config`, that are useful to access within views and22:doc:`CLI commands </cli>`. However, importing the ``app`` instance23within the modules in your project is prone to circular import issues.24When using the :doc:`app factory pattern </patterns/appfactories>` or25writing reusable :doc:`blueprints </blueprints>` or26:doc:`extensions </extensions>` there won't be an ``app`` instance to27import at all.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 28why, one step back28Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 29–31why, one step back29Flask solves this issue with the *application context*. Rather than30referring to an ``app`` directly, you use the the :data:`current_app`31proxy, which points to the application handling the current activity.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 32why, one step back32Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 33–35why, one step back33Flask automatically *pushes* an application context when handling a34request. View functions, error handlers, and other functions that run35during a request will have access to :data:`current_app`.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 36why, one step back36Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 37–38why, one step back37Flask will also automatically push an app context when running CLI38commands registered with :attr:`Flask.cli` using ``@app.cli.command()``.fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 39–40why, one step back3940Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 41–42why, one step back41Lifetime of the Context42-----------------------fix doc build errorDavid Lordbefore adoptiond2a46dc
line 43why, one step back43Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 44–49why, one step back44The application context is created and destroyed as necessary. When a45Flask application begins handling a request, it pushes an application46context and a :doc:`request context </reqcontext>`. When the request47ends it pops the request context then the application context.48Typically, an application context will have the same lifetime as a49request.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 50why, one step back50Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 51–52why, one step back51See :doc:`/reqcontext` for more information about how the contexts work52and the full lifecycle of a request.fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 53–54why, one step back5354Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 55–56why, one step back55Manually Push a Context56-----------------------fix doc build errorDavid Lordbefore adoptiond2a46dc
line 57why, one step back57Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 58–59why, one step back58If you try to access :data:`current_app`, or anything that uses it,59outside an application context, you'll get this error message:fix doc build errorDavid Lordbefore adoptiond2a46dc
line 60why, one step back60Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
line 61why, one step back61.. code-block:: pytbfix doc build errorDavid Lordbefore adoptiond2a46dc
line 62why, one step back62Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
line 63why, one step back63 RuntimeError: Working outside of application context.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 64why, one step back64Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 65–67why, one step back65 This typically means that you attempted to use functionality that66 needed to interface with the current application object in some way.67 To solve this, set up an application context with app.app_context().fix doc build errorDavid Lordbefore adoptiond2a46dc
line 68why, one step back68Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 69–73why, one step back69If you see that error while configuring your application, such as when70initializing an extension, you can push a context manually since you71have direct access to the ``app``. Use :meth:`~Flask.app_context` in a72``with`` block, and everything that runs in the block will have access73to :data:`current_app`. ::fix doc build errorDavid Lordbefore adoptiond2a46dc
line 74why, one step back74Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 75–76why, one step back75 def create_app():76 app = Flask(__name__)fix doc build errorDavid Lordbefore adoptiond2a46dc
line 77why, one step back77Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 78–79why, one step back78 with app.app_context():79 init_db()fix doc build errorDavid Lordbefore adoptiond2a46dc
line 80why, one step back80Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
line 81why, one step back81 return appfix doc build errorDavid Lordbefore adoptiond2a46dc
line 82why, one step back82Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 83–85why, one step back83If you see that error somewhere else in your code not related to84configuring the application, it most likely indicates that you should85move that code into a view function or CLI command.fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 86–87why, one step back8687Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 88–89why, one step back88Storing Data89------------fix doc build errorDavid Lordbefore adoptiond2a46dc
line 90why, one step back90Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 91–94why, one step back91The application context is a good place to store common data during a92request or CLI command. Flask provides the :data:`g object <g>` for this93purpose. It is a simple namespace object that has the same lifetime as94an application context.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 95why, one step back95Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 96–101why, one step back96.. note::97 The ``g`` name stands for "global", but that is referring to the98 data being global *within a context*. The data on ``g`` is lost99 after the context ends, and it is not an appropriate place to store100 data between requests. Use the :data:`session` or a database to101 store data across requests.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 102why, one step back102Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
line 103why, one step back103A common use for :data:`g` is to manage resources during a request.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 104why, one step back104Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 105–109why, one step back1051. ``get_X()`` creates resource ``X`` if it does not exist, caching it106 as ``g.X``.1072. ``teardown_X()`` closes or otherwise deallocates the resource if it108 exists. It is registered as a :meth:`~Flask.teardown_appcontext`109 handler.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 110why, one step back110Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
line 111why, one step back111For example, you can manage a database connection using this pattern::fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 112–115why, one step back112113 from flask import g114115 def get_db():Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 116–117why, one step back116 if 'db' not in g:117 g.db = connect_to_database()fix doc build errorDavid Lordbefore adoptiond2a46dc
line 118why, one step back118Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 119–120why, one step back119 return g.db120fix doc build errorDavid Lordbefore adoptiond2a46dc
line 121why, one step back121 @app.teardown_appcontextMerge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 122–124why, one step back122 def teardown_db():123 db = g.pop('db', None)124fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 125–127why, one step back125 if db is not None:126 db.close()127Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 128–130why, one step back128During a request, every call to ``get_db()`` will return the same129connection, and it will be closed automatically at the end of the130request.fix doc build errorDavid Lordbefore adoptiond2a46dc
line 131why, one step back131Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 132–134why, one step back132You can use :class:`~werkzeug.local.LocalProxy` to make a new context133local from ``get_db()``::134fix doc build errorDavid Lordbefore adoptiond2a46dc
lines 135–137why, one step back135 from werkzeug.local import LocalProxy136 db = LocalProxy(get_db)137Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6
lines 138–159why, one step back138Accessing ``db`` will call ``get_db`` internally, in the same way that139:data:`current_app` works.140141----142143If you're writing an extension, :data:`g` should be reserved for user144code. You may store internal data on the context itself, but be sure to145use a sufficiently unique name. The current context is accessed with146:data:`_app_ctx_stack.top <_app_ctx_stack>`. For more information see147:doc:`extensiondev`.148149150Events and Signals151------------------152153The application will call functions registered with154:meth:`~Flask.teardown_appcontext` when the application context is155popped.156157If :data:`~signals.signals_available` is true, the following signals are158sent: :data:`appcontext_pushed`, :data:`appcontext_tearing_down`, and159:data:`appcontext_popped`.