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=src_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` = None |
| _cv_token | `contextvars.Token[AppContext] | None` = None |
| _push_count | int = 0 | Track 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=src_flask_app_flask),
request: Request | None = None,
session: SessionMixin | None = None
) - > None
Parameters
| Name | Type | Description |
|---|
| app | [Flask](../app/flask.md?sid=src_flask_app_flask) | The Flask application instance this context represents. |
| request | `Request | None` = None |
| session | `SessionMixin | None` = None |
Methods
from_environ()
@classmethod
def from_environ(
app: [Flask](../app/flask.md?sid=src_flask_app_flask),
environ: WSGIEnvironment
) - > [AppContext](appcontext.md?sid=src_flask_ctx_appcontext)
Create an app context with request data from the given WSGI environ.
Parameters
| Name | Type | Description |
|---|
| app | [Flask](../app/flask.md?sid=src_flask_app_flask) | The application this context represents. |
| environ | WSGIEnvironment | The request data this context represents. |
Returns
| Type | Description |
|---|
[AppContext](appcontext.md?sid=src_flask_ctx_appcontext) | A new instance of AppContext initialized with the provided WSGI environment |
has_request()
@classmethod
def has_request() - > bool
True if this context was created with request data.
Returns
| Type | Description |
|---|
bool | True if the context contains request information, False otherwise |
copy()
@classmethod
def copy() - > [AppContext](appcontext.md?sid=src_flask_ctx_appcontext)
Create a new context with the same data objects as this context. See :func:.copy_current_request_context.
Returns
| Type | Description |
|---|
[AppContext](appcontext.md?sid=src_flask_ctx_appcontext) | A new AppContext instance sharing the same application, request, and session data |
request()
@classmethod
def request() - > [Request](../wrappers/request.md?sid=src_flask_wrappers_request)
The request object associated with this context. Accessed through :data:.request. Only available in request contexts, otherwise raises :exc:RuntimeError.
Returns
| Type | Description |
|---|
[Request](../wrappers/request.md?sid=src_flask_wrappers_request) | The Request object for the current context |
session()
@classmethod
def session() - > [SessionMixin](../sessions/sessionmixin.md?sid=src_flask_sessions_sessionmixin)
The session object associated with this context. Accessed through :data:.session. Only available in request contexts, otherwise raises :exc:RuntimeError. Accessing this sets :attr:.SessionMixin.accessed.
Returns
| Type | Description |
|---|
[SessionMixin](../sessions/sessionmixin.md?sid=src_flask_sessions_sessionmixin) | The session object with its accessed flag set to True |
match_request()
@classmethod
def match_request() - > None
Apply routing to the current request, storing either the matched endpoint and args, or a routing exception.
Returns
push()
@classmethod
def push() - > None
Push this context so that it is the active context. If this is a request context, calls :meth:match_request to perform routing with the context active.
Returns
pop()
@classmethod
def pop(
exc: BaseException | None
) - > None
Pop this context so that it is no longer the active context. Then call teardown functions and signals.
Parameters
| Name | Type | Description |
|---|
| exc | `BaseException | None` |
Returns