An app context contains information about an app, and about the request when handling a request. A context is pushed at the beginning of each request and CLI command, and popped at the end. The context is referred to as a "request context" if it has request information, and an "app context" if not.
Attributes
| Attribute | Type | Description |
|---|
| app | [Flask](../app/flask.md?sid=flask_app_flask) | The application represented by this context. Accessed through :data:.current_app. |
| g | _AppCtxGlobals | The global data for this context. Accessed through :data:.g. |
| url_adapter | `MapAdapter | None` |
| _request | `Request | None` |
| _session | `SessionMixin | None` |
| _flashes | `list[tuple[str, str]] | None` |
| _after_request_functions | list[ft.AfterRequestCallable[t.Any]] | Stores a list of functions to be called after the request has been processed. |
| _cv_token | `contextvars.Token[AppContext] | None` |
| _push_count | int | Tracks nested pushes of this context; cleanup will only run once the original push has been popped. |
Constructor
Signature
def AppContext(
app: [Flask](../app/flask.md?sid=flask_app_flask),
request: Request | None = None,
session: SessionMixin | None = None
) - > null
Parameters
| Name | Type | Description |
|---|
| app | [Flask](../app/flask.md?sid=flask_app_flask) | The application this context represents. |
| request | `Request | None` = None |
| session | `SessionMixin | None` = None |
Methods
from_environ()
@classmethod
def from_environ(
app: [Flask](../app/flask.md?sid=flask_app_flask),
environ: WSGIEnvironment
) - > te.Self
Create an app context with request data from the given WSGI environ.
Parameters
| Name | Type | Description |
|---|
| app | [Flask](../app/flask.md?sid=flask_app_flask) | The application this context represents. |
| environ | WSGIEnvironment | The request data this context represents. |
Returns
| Type | Description |
|---|
te.Self | A new AppContext instance initialized with the provided application and request data. |
has_request()
@classmethod
def has_request() - > bool
Checks if this context was created with request data.
Returns
| Type | Description |
|---|
bool | True if the context contains request data, False otherwise. |
copy()
@classmethod
def copy() - > te.Self
Create a new context with the same data objects as this context. See :func:.copy_current_request_context.
Returns
| Type | Description |
|---|
te.Self | A new AppContext instance that is a shallow copy of the current context. |
request()
@classmethod
def request() - > [Request](../wrappers/request.md?sid=flask_wrappers_request)
Provides access to the request object associated with this context. This property is only available in request contexts; accessing it in an app-only context will raise a RuntimeError.
Returns
| Type | Description |
|---|
[Request](../wrappers/request.md?sid=flask_wrappers_request) | The Request object for the current context. |
session()
@classmethod
def session() - > [SessionMixin](../sessions/sessionmixin.md?sid=flask_sessions_sessionmixin)
Provides access to the session object associated with this context. This property is only available in request contexts; accessing it in an app-only context will raise a RuntimeError. Accessing this property also sets the accessed flag on the session.
Returns
| Type | Description |
|---|
[SessionMixin](../sessions/sessionmixin.md?sid=flask_sessions_sessionmixin) | The session object for the current context. |
match_request()
@classmethod
def match_request() - > None
Applies routing to the current request, determining the matched endpoint and arguments or storing a routing exception. This prepares the request for processing by the appropriate view function.
Returns
| Type | Description |
|---|
None | This method does not return a value; it modifies the internal state of the request object. |
push()
@classmethod
def push() - > None
Pushes this context onto the context stack, making it the active context. If this is a request context, it also performs URL routing. This method is typically managed by a with block.
Returns
| Type | Description |
|---|
None | This method does not return a value; it modifies the global context stack. |
pop()
@classmethod
def pop(
exc: BaseException | None = None
) - > None
Pops this context from the context stack, deactivating it and triggering teardown functions and signals. This method is typically managed by a with block.
Parameters
| Name | Type | Description |
|---|
| exc | `BaseException | None` = None |
Returns
| Type | Description |
|---|
None | This method does not return a value; it modifies the global context stack and performs cleanup. |