quart.ctx module#

class quart.ctx.RequestContext(app, request, session=None)#

Bases: _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.

Parameters:
  • app (Quart)

  • request (Request)

  • session (SessionMixin | None)

_after_request_functions#

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

property request: Request#
async push()#
Return type:

None

async pop(exc=_sentinel)#
Parameters:

exc (BaseException | None)

Return type:

None

class quart.ctx.WebsocketContext(app, request, session=None)#

Bases: _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.

Parameters:
_after_websocket_functions#

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

property websocket: Websocket#
async push()#
Return type:

None

async pop(exc=_sentinel)#
Parameters:

exc (BaseException | None)

Return type:

None

class quart.ctx.AppContext(app)#

Bases: object

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

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

Parameters:

app (Quart)

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()#
Return type:

AppContext

async push()#
Return type:

None

async pop(exc=_sentinel)#
Parameters:

exc (BaseException | None)

Return type:

None

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