チュートリアル - ユーザーガイド¶
このチュートリアルでは、FastAPI のほとんどの機能をステップバイステップで利用する方法を説明します。
各セクションは前のセクションを基に徐々に構築されていますが、トピックごとに分離されているため、特定の API のニーズを解決するために直接特定のセクションに移動できます。
また、将来の参考資料としても機能するように構築されており、必要なものを正確に見に戻ることができます。
コードを実行する¶
すべてのコードブロックは直接コピーして使用できます (実際にはテスト済みの Python ファイルです)。
いずれかの例を実行するには、コードを `main.py` ファイルにコピーし、`fastapi dev` を使用して開始します。
$ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:solid">main.py</u>
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting development server 🚀
Searching for package file structure from directories
with <font color="#3465A4">__init__.py</font> files
Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font>
<span style="background-color:#007166"><font color="#D3D7CF"> module </font></span> 🐍 main.py
<span style="background-color:#007166"><font color="#D3D7CF"> code </font></span> Importing the FastAPI app object from the module with
the following code:
<u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u>
<span style="background-color:#007166"><font color="#D3D7CF"> app </font></span> Using import string: <font color="#3465A4">main:app</font>
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font>
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000/docs</u></font>
<span style="background-color:#007166"><font color="#D3D7CF"> tip </font></span> Running in development mode, for production use:
<b>fastapi run</b>
Logs:
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Will watch for changes in these directories:
<b>[</b><font color="#4E9A06">'/home/user/code/awesomeapp'</font><b>]</b>
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C
to quit<b>)</b>
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b>
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
コードを記述またはコピーし、編集してローカルで実行することを強くお勧めします。
エディタでそれを使用することで、FastAPI の利点が真に示され、記述する必要のあるコードの少なさ、すべての型チェック、オートコンプリートなどが確認できます。
FastAPI のインストール¶
最初のステップは FastAPI のインストールです。
仮想環境を作成し、それをアクティブ化してから、FastAPI をインストールしてください。
$ pip install "fastapi[standard]"
---> 100%
注記
pip install "fastapi[standard]"
でインストールすると、FastAPI Cloud にデプロイできる fastapi-cloud-cli
を含む、いくつかのデフォルトのオプションの標準依存関係が付属しています。
これらのオプションの依存関係が不要な場合は、代わりに pip install fastapi
をインストールできます。
標準の依存関係はインストールしたいが、fastapi-cloud-cli
は不要な場合は、pip install "fastapi[standard-no-fastapi-cloud-cli]"
でインストールできます。
上級ユーザーガイド¶
この チュートリアル - ユーザーガイド を読んだ後で、上級ユーザーガイド も読むことができます。
上級ユーザーガイド はこのガイドを基にしており、同じ概念を使用し、いくつかの追加機能を教えてくれます。
しかし、最初に チュートリアル - ユーザーガイド (今あなたが読んでいるもの) を読むべきです。
これは、チュートリアル - ユーザーガイド だけで完全なアプリケーションを構築し、その後、上級ユーザーガイド からの追加のアイデアを使用して、必要に応じてさまざまな方法で拡張できるように設計されています。