Flask Internal Objects API
Flask provides several low-level internal objects for accessing the active application context and blueprint registration state. Most applications do not need to use them directly.
AppContext application context
| Methods/Attributes | Description |
|---|---|
| AppContext(app) | Create an application context instance. Usually not created manually; use app.app_context() |
| push() | Push onto the context stack. Make proxies such as current_app and g point to this context. |
| pop(error=None) | Pop the context stack. Execute teardown callbacks. error is the unhandled exception object. |
| app | The Flask application associated with this context. |
| g | The g object of this context (an _AppCtxGlobals instance) |
| url_adapter | URL adapter (MapAdapter), used for URL building |
| request | The current request object (Request). Only available in the request context. |
| session | The current Session object. Only available in the request context. |
app_ctx proxy
| Property | Description |
|---|---|
| flask.globals.app_ctx | A proxy pointing to the currently active AppContext. For internal use; in most cases, current_app and g are sufficient. |
BlueprintSetupState
The state object for blueprint registration, available in the record callback.
| Property | Description |
|---|---|
| app | The Flask application to which the blueprint is registered |
| blueprint | The registered Blueprint instance |
| url_prefix | The URL prefix of the blueprint |
| subdomain | The blueprint's subdomain |
| url_defaults | The URL defaults of the blueprint |
| options | Extra options passed in when registering the blueprint |
Deprecated RequestContext
Since Flask 3.2,RequestContexthas been merged intoAppContext. Relatedrequest_ctxProxies have also been merged intoapp_ctxThe old name will be removed in Flask 4.0.
Code Examples
Example
from flask import Flask
from flask.ctx import AppContext
app = Flask(__name__)
# Manually manage the application context
ctx = app.app_context()
ctx.push()
# current_app and g are now available
from flask import current_app, g
g.user = "example"
# Perform operations...
ctx.pop() # Trigger the teardown callback, g is cleaned up
# with statement approach (recommended)
with app.app_context():
g.temp_data = 42
# Automatically pop after exiting with
from flask.ctx import AppContext
app = Flask(__name__)
# Manually manage the application context
ctx = app.app_context()
ctx.push()
# current_app and g are now available
from flask import current_app, g
g.user = "example"
# Perform operations...
ctx.pop() # Trigger the teardown callback, g is cleaned up
# with statement approach (recommended)
with app.app_context():
g.temp_data = 42
# Automatically pop after exiting with