教程 - 用户指南¶
本教程将逐步展示如何使用 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-cli
,这允许您部署到 FastAPI Cloud。
如果您不希望包含这些可选依赖项,则可以安装 pip install fastapi
。
如果您想安装标准依赖项,但不包含 fastapi-cloud-cli
,可以使用 pip install "fastapi[standard-no-fastapi-cloud-cli]"
进行安装。
高级用户指南¶
还有一个 高级用户指南,您可以在阅读完本 教程 - 用户指南 后继续阅读。
高级用户指南 建立在此基础上,使用相同的概念,并教授您一些额外的功能。
但您应该首先阅读 教程 - 用户指南(您现在正在阅读的这份)。
它的设计目标是,您可以通过仅使用 教程 - 用户指南 来构建一个完整的应用程序,然后根据您的需求,使用 高级用户指南 中的一些额外想法,以不同的方式扩展它。