Skip to main content

AppContext

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

AttributeTypeDescription
app[Flask](../app/flask.md?sid=flask_app_flask)The application represented by this context. Accessed through :data:.current_app.
g_AppCtxGlobalsThe global data for this context. Accessed through :data:.g.
url_adapter`MapAdapterNone`
_request`RequestNone`
_session`SessionMixinNone`
_flashes`list[tuple[str, str]]None`
_after_request_functionslist[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_countintTracks 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

NameTypeDescription
app[Flask](../app/flask.md?sid=flask_app_flask)The application this context represents.
request`RequestNone` = None
session`SessionMixinNone` = 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

NameTypeDescription
app[Flask](../app/flask.md?sid=flask_app_flask)The application this context represents.
environWSGIEnvironmentThe request data this context represents.

Returns

TypeDescription
te.SelfA 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

TypeDescription
boolTrue 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

TypeDescription
te.SelfA 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

TypeDescription
[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

TypeDescription
[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

TypeDescription
NoneThis 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

TypeDescription
NoneThis 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

NameTypeDescription
exc`BaseExceptionNone` = None

Returns

TypeDescription
NoneThis method does not return a value; it modifies the global context stack and performs cleanup.