quart.ctx module

class quart.ctx.AppContext(app: Quart)

Bases: object

The context relating to the app bound to the current task.

Do not use directly, prefer the app_context() instead.

app

The app itself.

url_adapter

An adapter bound to the server, but not a specific task, useful for route building.

g

An instance of the ctx globals class.

copy() → AppContext
async pop(exc: Optional[BaseException]) → None
async push() → None
class quart.ctx.RequestContext(app: Quart, request: quart.wrappers.request.Request)

Bases: quart.ctx._BaseRequestWebsocketContext

The context relating to the specific request, bound to the current task.

Do not use directly, prefer the request_context() or test_request_context() instead.

_after_request_functions

List of functions to execute after the curret request, see after_this_request().

property request
class quart.ctx.WebsocketContext(app: Quart, request: quart.wrappers.request.Websocket)

Bases: quart.ctx._BaseRequestWebsocketContext

The context relating to the specific websocket, bound to the current task.

Do not use directly, prefer the websocket_context() or test_websocket_context() instead.

_after_websocket_functions

List of functions to execute after the curret websocket, see after_this_websocket().

property websocket
quart.ctx.after_this_request(func: Callable) → Callable

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

    ...
quart.ctx.after_this_websocket(func: Callable) → Callable

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

    ...
quart.ctx.copy_current_app_context(func: Callable) → Callable

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
    ...
quart.ctx.copy_current_request_context(func: Callable) → Callable

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
    ...
quart.ctx.copy_current_websocket_context(func: Callable) → Callable

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
    ...
quart.ctx.has_app_context() → bool

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()

quart.ctx.has_request_context() → bool

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().

quart.ctx.has_websocket_context() → bool

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().