0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NiceGUIでFastAPIのRequestオブジェクトを取得する

Posted at

NiceGUIでは、FastAPIのリクエストオブジェクトを取得できます。

実行環境

  • Python 3.10
  • NiceGUI 1.3.13

コード

from fastapi import Request
from nicegui import ui

@ui.page('/')
def main_page(request: Request):
    # リクエストオブジェクトを使った処理
    ui.label(f'Hello {request.client.host}!')

ui.run()

このコードのポイントは、引数にrequest: RequestとFastAPIの型注釈付きで指定している点です。ここは内部的にPydanticでチェックされているようで、引数名や型が違うと正常に動作しません。

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?