Skip to content
Myr

appcontext.rst

This file

Path

docs/appcontext.rst

159 lines · 5.4 KiB

Last changed by

Merge branch '1.0.x'David Lordbefore adoption2236ba9

History of this file

Take it

The walk stopped after 200 commits: lines older than 851eaa4 point at it.

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 1–2why, one step back
1.. currentmodule:: flask
2

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 3–7why, one step back
3.. _app-context:
45The Application Context6=======================7

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 8–11why, one step back
8The application context keeps track of the application-level data during
9a 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.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 12why, one step back
12

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 13–15why, one step back
13This is similar to the :doc:`/reqcontext`, which keeps track of
14request-level data during a request. A corresponding application context15is pushed when a request context is pushed.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 16why, one step back
16

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 17–18why, one step back
17Purpose of the Context
18----------------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 19why, one step back
19

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 20–27why, one step back
20The :class:`Flask` application object has attributes, such as
21: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.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 28why, one step back
28

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 29why, one step back
29Flask solves this issue with the *application context*. Rather than

Fix typo in appcontext.rstSaul Uriasbefore adoption0b4296f

line 30why, one step back
30referring to an ``app`` directly, you use the :data:`current_app`

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 31why, one step back
31proxy, which points to the application handling the current activity.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 32why, one step back
32

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 33–35why, one step back
33Flask automatically *pushes* an application context when handling a
34request. View functions, error handlers, and other functions that run35during a request will have access to :data:`current_app`.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 36why, one step back
36

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 37–38why, one step back
37Flask will also automatically push an app context when running CLI
38commands registered with :attr:`Flask.cli` using ``@app.cli.command()``.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 39–40why, one step back
39
40

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 41–42why, one step back
41Lifetime of the Context
42-----------------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 43why, one step back
43

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 44–49why, one step back
44The application context is created and destroyed as necessary. When a
45Flask 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.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 50why, one step back
50

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 51why, one step back
51See :doc:`/reqcontext` for more information about how the contexts work

Merge branch '1.0.x'David Lordbefore adoption2236ba9

line 52why, one step back
52and the full life cycle of a request.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 53–54why, one step back
53
54

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 55–56why, one step back
55Manually Push a Context
56-----------------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 57why, one step back
57

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 58–59why, one step back
58If you try to access :data:`current_app`, or anything that uses it,
59outside an application context, you'll get this error message:

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 60why, one step back
60

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 61why, one step back
61.. code-block:: pytb

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 62why, one step back
62

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 63why, one step back
63 RuntimeError: Working outside of application context.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 64why, one step back
64

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 65–67why, one step back
65 This typically means that you attempted to use functionality that
66 needed to interface with the current application object in some way.67 To solve this, set up an application context with app.app_context().

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 68why, one step back
68

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 69–73why, one step back
69If you see that error while configuring your application, such as when
70initializing 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`. ::

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 74why, one step back
74

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 75–76why, one step back
75 def create_app():
76 app = Flask(__name__)

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 77why, one step back
77

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 78–79why, one step back
78 with app.app_context():
79 init_db()

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 80why, one step back
80

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 81why, one step back
81 return app

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 82why, one step back
82

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 83–85why, one step back
83If you see that error somewhere else in your code not related to
84configuring the application, it most likely indicates that you should85move that code into a view function or CLI command.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 86–87why, one step back
86
87

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 88–89why, one step back
88Storing Data
89------------

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 90why, one step back
90

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 91–94why, one step back
91The application context is a good place to store common data during a
92request 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.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 95why, one step back
95

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 96–101why, one step back
96.. 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.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 102why, one step back
102

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 103why, one step back
103A common use for :data:`g` is to manage resources during a request.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 104why, one step back
104

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 105–109why, one step back
1051. ``get_X()`` creates resource ``X`` if it does not exist, caching it
106 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.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 110why, one step back
110

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

line 111why, one step back
111For example, you can manage a database connection using this pattern::

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 112–115why, one step back
112
113 from flask import g114115 def get_db():

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 116–117why, one step back
116 if 'db' not in g:
117 g.db = connect_to_database()

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 118why, one step back
118

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 119–120why, one step back
119 return g.db
120

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 121why, one step back
121 @app.teardown_appcontext

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 122–124why, one step back
122 def teardown_db():
123 db = g.pop('db', None)124

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 125–127why, one step back
125 if db is not None:
126 db.close()127

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 128–130why, one step back
128During a request, every call to ``get_db()`` will return the same
129connection, and it will be closed automatically at the end of the130request.

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

line 131why, one step back
131

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 132–134why, one step back
132You can use :class:`~werkzeug.local.LocalProxy` to make a new context
133local from ``get_db()``::134

Merge pull request #2416 from davidism/dotenvDavid Lordbefore adoption851eaa4

lines 135–137why, one step back
135 from werkzeug.local import LocalProxy
136 db = LocalProxy(get_db)137

Merge pull request #2685 from davidism/app-context-docsDavid Lordbefore adoption16d83d6

lines 138–159why, one step back
138Accessing ``db`` will call ``get_db`` internally, in the same way that
139: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`.