Describing the Application Context
-
An application context tracks application-level data from:
- A request
- A CLI command
- Other activity
- Flask avoids passing an application to each function
- It does this by creating objects
gandcurrent_app - These are proxies that can be accessed at any point
Issue with Importing app Instances
- Every
Flaskapplication object has attributes - The
configattribute is one example - Attributes are useful to access within views and CLI commands
- However, importing an
appinstance within a module is prone to circular imports - An
appinstance can't be imported using the app factory pattern - Flask solves this problem with the application context
The Purpose of the Application Context
- With application contexts, we don't refer to an
appdirectly - Instead, we use a
current_appproxy - This points to the application handling the current activity
- Flask automatically pushes an application context when handling a request
-
The following functions have access to
current_app:- View functions
- Error handlers
- Other functions
Defining the Structure of Application Contexts
- There is a stack that manages application contexts
-
This stack is referred to as the application context stack
- In the code, this is
_app_ctx_stack
- In the code, this is
-
Eac element in the stack is a single application context
- In the code, this is an
AppContextclass
- In the code, this is an
-
Each application context is essentially a list of two objects:
- A single
gobject - A single
current_appobject
- A single
Implementing an Application Context in Flask
- Flask needs a
Flaskobject to create an application context - Flask needs an environment dictionary to create a request context
- The environment dictionary has all the client data and is taken from the browser
- The applications and request context are both created simultaneously
-
The steps include the following:
- A
Flaskobject is created - An application context is created when an
appvariable is assigned - A request context is then created when getting an environment dictionary
- View functions are invoked
- Gain access to objects
g,current_app,request, andsession
- A
Lifetieme of the Context
- The application context is created and destroyed as necessary
- A Flask application begins by handling a request
- Then, it pushes an application context and a request context
-
Once a request ends, it does the following:
- Pops the request context
- Then, pops the application context
- Typically, an application context will have the same lifetime as a request
Storing Data with the Application Context
- The application context is used for storing common data
- Specifically, this data refers to a request or CLI command
-
Flask provides the
gobject for this purposegstands forglobal- This refers to data being global within a context
- The data on
gis lost after the context ends -
Implying, it is not used for storing data between requests
- To store data across requests, we can use
sessionor store data in a database
- To store data across requests, we can use
- It is a simple namespace object that has the same lifetime as an application context
References
Previous
Next