quart.ctx モジュール#

class quart.ctx.AppContext(app: Quart)#

ベースクラス: object

現在のタスクにバインドされたアプリに関連するコンテキスト。

直接使用しないでください。app_context() を代わりに使用してください。

app#

アプリ自体。

url_adapter#

サーバーにバインドされていますが、特定のタスクにはバインドされていないアダプター。ルート構築に役立ちます。

g#

ctx グローバルクラスのインスタンス。

copy() AppContext#
async pop(exc: BaseException | None = <object object>) None#
async push() None#
class quart.ctx.RequestContext(app: Quart, request: Request, session: SessionMixin | None = None)#

ベースクラス: _BaseRequestWebsocketContext

現在のタスクにバインドされた、特定のリクエストに関連するコンテキスト。

直接使用しないでください。request_context() または test_request_context() を代わりに使用してください。

_after_request_functions#

現在リクエスト後に実行する関数のリスト。 after_this_request() を参照。

async pop(exc: BaseException | None = <object object>) None#
async push() None#
property request: Request#
class quart.ctx.WebsocketContext(app: Quart, request: Websocket, session: SessionMixin | None = None)#

ベースクラス: _BaseRequestWebsocketContext

現在のタスクにバインドされた、特定のwebsocketに関連するコンテキスト。

直接使用しないでください。websocket_context() または test_websocket_context() を代わりに使用してください。

_after_websocket_functions#

現在websocket後に実行する関数のリスト。 after_this_websocket() を参照。

async pop(exc: BaseException | None = <object object>) None#
async push() None#
property websocket: Websocket#
quart.ctx.after_this_request(func: AfterRequestCallable) AfterRequestCallable#

現在リクエスト後に呼び出されるように func をスケジュールします。

これは、特定のルートまたは状況のみに対するリクエスト後関数を必要とする場合に役立ちます。例:

def index():
    @after_this_request
    def set_cookie(response):
        response.set_cookie('special', 'value')
        return response

    ...
quart.ctx.after_this_websocket(func: AfterWebsocketCallable) AfterWebsocketCallable#

現在websocket後に呼び出されるように func をスケジュールします。

これは、特定のルートまたは状況のみに対するwebsocket後関数を必要とする場合に役立ちます。例:

注記

レスポンスはオプションの引数であり、websocketがアクティブでない場合(つまり、エラーが発生した場合)にのみ渡されます。

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#

現在のアプリコンテキストをデコレートされた関数と共有します。

アプリコンテキストはタスクごとにローカルであるため、他のタスクでは使用できません。このデコレーターを使用してコンテキストを使用可能にすることができます。

@copy_current_app_context
async def within_context() -> None:
    name = current_app.name
    ...
quart.ctx.copy_current_request_context(func: Callable) Callable#

現在のリクエストコンテキストをデコレートされた関数と共有します。

リクエストコンテキストはタスクごとにローカルであるため、他のタスクでは使用できません。このデコレーターを使用してコンテキストを使用可能にすることができます。

@copy_current_request_context
async def within_context() -> None:
    method = request.method
    ...
quart.ctx.copy_current_websocket_context(func: Callable) Callable#

デコレートされた関数に現在のwebsocketコンテキストを共有します。

websocketコンテキストはタスクごとにローカルであるため、他のタスクでは利用できません。このデコレータを使用して、コンテキストを利用可能にすることができます。

@copy_current_websocket_context
async def within_context() -> None:
    method = websocket.method
    ...
quart.ctx.has_app_context() bool#

実行がアプリケーションコンテキスト内にあるかどうかをチェックします。

これにより、アプリケーションコンテキストが利用可能な場合は制御された方法で動作し、利用できない場合は黙って動作しないようにすることができます。例えば、

if has_app_context():
    log.info("Executing in %s context", current_app.name)

参照: has_request_context()

quart.ctx.has_request_context() bool#

実行がリクエストコンテキスト内にあるかどうかをチェックします。

これにより、リクエストコンテキストが利用可能な場合は制御された方法で動作し、利用できない場合は黙って動作しないようにすることができます。例えば、

if has_request_context():
    log.info("Request endpoint %s", request.endpoint)

参照: has_app_context()

quart.ctx.has_websocket_context() bool#

実行がwebsocketコンテキスト内にあるかどうかをチェックします。

これにより、websocketコンテキストが利用可能な場合は制御された方法で動作し、利用できない場合は黙って動作しないようにすることができます。例えば、

if has_websocket_context():
    log.info("Websocket endpoint %s", websocket.endpoint)

参照: has_app_context()