quart.ctx module
- class quart.ctx.AppContext(app, *, request=None, session=None, websocket=None)
Bases:
object- Parameters:
- property has_request: bool
True if this context was created with request data.
- property has_websocket: bool
True if this context was created with request data.
- copy()
Create a new context with the same data objects as this context. See
copy_current_request_context().- Return type:
Self
- property request: Request
The request object associated with this context. Accessed through
request. Only available in request contexts, otherwise raisesRuntimeError.
- property websocket: Websocket
The websocket object associated with this context. Accessed through
websocket. Only available in websocket contexts, otherwise raisesRuntimeError.
- property session: SessionMixin
The session object associated with this context. Accessed through
session. Only available in request contexts, otherwise raisesRuntimeError. Accessing this setsSessionMixin.accessed.
- match_request()
Apply routing to the current request, storing either the matched endpoint and args, or a routing exception.
- Return type:
None
- async push()
Push this context so that it is the active context. If this is a request context, calls
match_request()to perform routing with the context active.Typically, this is not used directly. Instead, use a
withblock to manage the context.In some situations, such as streaming or testing, the context may be pushed multiple times. It will only trigger matching and signals if it is not currently pushed.
- Return type:
None
- async pop(exc=None)
Pop this context so that it is no longer the active context. Then call teardown functions and signals.
Typically, this is not used directly. Instead, use a
withblock to manage the context.This context must currently be the active context, otherwise a
RuntimeErroris raised. In some situations, such as streaming or testing, the context may have been pushed multiple times. It will only trigger cleanup once it has been popped as many times as it was pushed. Until then, it will remain the active context.- Parameters:
exc (BaseException | None) – An unhandled exception that was raised while the context was active. Passed to teardown functions.
- Return type:
None
Changed in version 0.9: Added the
excargument.
- quart.ctx.after_this_request(func)
Schedule the func to be called after the current request.
This is useful in situations whereby you want an after request function for a specific route or circumstance only, for example,
def index(): @after_this_request def set_cookie(response): response.set_cookie('special', 'value') return response ...
- Parameters:
func (AfterRequestCallable)
- Return type:
AfterRequestCallable
- quart.ctx.after_this_websocket(func)
Schedule the func to be called after the current websocket.
This is useful in situations whereby you want an after websocket function for a specific route or circumstance only, for example,
Note
The response is an optional argument, and will only be passed if the websocket was not active (i.e. there was an error).
def index(): @after_this_websocket def set_cookie(response: Optional[Response]): response.set_cookie('special', 'value') return response ...
- Parameters:
func (AfterWebsocketCallable)
- Return type:
AfterWebsocketCallable
- quart.ctx.copy_current_app_context(func)
Share the current app context with the function decorated.
The app context is local per task and hence will not be available in any other task. This decorator can be used to make the context available,
@copy_current_app_context async def within_context() -> None: name = current_app.name ...
- Parameters:
func (Callable)
- Return type:
Callable
- quart.ctx.copy_current_request_context(func)
Share the current request context with the function decorated.
The request context is local per task and hence will not be available in any other task. This decorator can be used to make the context available,
@copy_current_request_context async def within_context() -> None: method = request.method ...
- Parameters:
func (Callable)
- Return type:
Callable
- quart.ctx.copy_current_websocket_context(func)
Share the current websocket context with the function decorated.
The websocket context is local per task and hence will not be available in any other task. This decorator can be used to make the context available,
@copy_current_websocket_context async def within_context() -> None: method = websocket.method ...
- Parameters:
func (Callable)
- Return type:
Callable
- quart.ctx.has_app_context()
Check if execution is within an app context.
This allows a controlled way to act if there is an app context available, or silently not act if not. For example,
if has_app_context(): log.info("Executing in %s context", current_app.name)
See also
has_request_context()- Return type:
bool
- quart.ctx.has_request_context()
Check if execution is within a request context.
This allows a controlled way to act if there is a request context available, or silently not act if not. For example,
if has_request_context(): log.info("Request endpoint %s", request.endpoint)
See also
has_app_context().- Return type:
bool
- quart.ctx.has_websocket_context()
Check if execution is within a websocket context.
This allows a controlled way to act if there is a websocket context available, or silently not act if not. For example,
if has_websocket_context(): log.info("Websocket endpoint %s", websocket.endpoint)
See also
has_app_context().- Return type:
bool