Home / Class/ AppContext Class — flask Architecture

AppContext Class — flask Architecture

Architecture documentation for the AppContext class in ctx.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9["AppContext"]
  49f8280b_d7dc_110c_b848_8e7e56bfb19b["ctx.py"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|defined in| 49f8280b_d7dc_110c_b848_8e7e56bfb19b
  31e318e5_adf8_900b_d893_adb4ea07a9b9["__init__()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 31e318e5_adf8_900b_d893_adb4ea07a9b9
  5fd70c3a_330e_eb7f_a826_2751b2ab0d3f["from_environ()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 5fd70c3a_330e_eb7f_a826_2751b2ab0d3f
  0517f101_32b9_0a74_629b_d49f7e9e82b0["has_request()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 0517f101_32b9_0a74_629b_d49f7e9e82b0
  b9619729_0242_23b5_d6f7_5f78607ae976["copy()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| b9619729_0242_23b5_d6f7_5f78607ae976
  1530ca58_4be8_223e_a4ab_7d8b2bd0153d["request()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 1530ca58_4be8_223e_a4ab_7d8b2bd0153d
  91a99133_b1f0_35e9_438c_7ae6016b40a9["session()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 91a99133_b1f0_35e9_438c_7ae6016b40a9
  5f896cf4_e98f_e9ed_264e_d22af8ed8eec["match_request()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 5f896cf4_e98f_e9ed_264e_d22af8ed8eec
  519276e3_fb95_dd4f_f8b0_6550aeb8f81b["push()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 519276e3_fb95_dd4f_f8b0_6550aeb8f81b
  c03b3eb7_5e70_0ee6_2d39_454cca5a0f61["pop()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| c03b3eb7_5e70_0ee6_2d39_454cca5a0f61
  5b74ca59_5b16_8055_3522_e83982aa2ae6["__enter__()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 5b74ca59_5b16_8055_3522_e83982aa2ae6
  8e27995b_4385_9444_eed6_e9c6966da15f["__exit__()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 8e27995b_4385_9444_eed6_e9c6966da15f
  4d210b8b_4c50_7c38_b9f2_62b6f7519420["__repr__()"]
  86817dcd_97b5_9a9f_aee8_5fa78b4cecc9 -->|method| 4d210b8b_4c50_7c38_b9f2_62b6f7519420

Relationship Graph

Source Code

src/flask/ctx.py lines 259–501

class 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.

    Do not use this class directly. Use :meth:`.Flask.app_context` to create an
    app context if needed during setup, and :meth:`.Flask.test_request_context`
    to create a request context if needed during tests.

    When the context is popped, it will evaluate all the teardown functions
    registered with :meth:`~flask.Flask.teardown_request` (if handling a
    request) then :meth:`.Flask.teardown_appcontext`.

    When using the interactive debugger, the context will be restored so
    ``request`` is still accessible. Similarly, the test client can preserve the
    context after the request ends. However, teardown functions may already have
    closed some resources such as database connections, and will run again when
    the restored context is popped.

    :param app: The application this context represents.
    :param request: The request data this context represents.
    :param session: The session data this context represents. If not given,
        loaded from the request on first access.

    .. versionchanged:: 3.2
        Merged with ``RequestContext``. The ``RequestContext`` alias will be
        removed in Flask 4.0.

    .. versionchanged:: 3.2
        A combined app and request context is pushed for every request and CLI
        command, rather than trying to detect if an app context is already
        pushed.

    .. versionchanged:: 3.2
        The session is loaded the first time it is accessed, rather than when
        the context is pushed.
    """

    def __init__(
        self,
        app: Flask,
        *,
        request: Request | None = None,
        session: SessionMixin | None = None,
    ) -> None:
        self.app = app
        """The application represented by this context. Accessed through
        :data:`.current_app`.
        """

        self.g: _AppCtxGlobals = app.app_ctx_globals_class()
        """The global data for this context. Accessed through :data:`.g`."""

        self.url_adapter: MapAdapter | None = None
        """The URL adapter bound to the request, or the app if not in a request.
        May be ``None`` if binding failed.
        """

        self._request: Request | None = request
        self._session: SessionMixin | None = session
        self._flashes: list[tuple[str, str]] | None = None
        self._after_request_functions: list[ft.AfterRequestCallable[t.Any]] = []

        try:
            self.url_adapter = app.create_url_adapter(self._request)
        except HTTPException as e:
            if self._request is not None:
                self._request.routing_exception = e

        self._cv_token: contextvars.Token[AppContext] | None = None
        """The previous state to restore when popping."""

        self._push_count: int = 0
        """Track nested pushes of this context. Cleanup will only run once the
        original push has been popped.
        """

    @classmethod
    def from_environ(cls, app: Flask, environ: WSGIEnvironment, /) -> te.Self:

Defined In

Frequently Asked Questions

What is the AppContext class?
AppContext is a class in the flask codebase, defined in src/flask/ctx.py.
Where is AppContext defined?
AppContext is defined in src/flask/ctx.py at line 259.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free