mirror of https://github.com/tiangolo/fastapi.git
📝 Add location info to `tutorial/bigger-applications.md` (#10552)
This commit is contained in:
parent
cb95d1cb89
commit
6bda1326a4
|
|
@ -79,7 +79,7 @@
|
|||
|
||||
👆 🗄 ⚫️ & ✍ "👐" 🎏 🌌 👆 🔜 ⏮️ 🎓 `FastAPI`:
|
||||
|
||||
```Python hl_lines="1 3"
|
||||
```Python hl_lines="1 3" title="app/routers/users.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/users.py!}
|
||||
```
|
||||
|
||||
|
|
@ -89,7 +89,7 @@
|
|||
|
||||
⚙️ ⚫️ 🎏 🌌 👆 🔜 ⚙️ `FastAPI` 🎓:
|
||||
|
||||
```Python hl_lines="6 11 16"
|
||||
```Python hl_lines="6 11 16" title="app/routers/users.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/users.py!}
|
||||
```
|
||||
|
||||
|
|
@ -112,7 +112,7 @@
|
|||
|
||||
👥 🔜 🔜 ⚙️ 🙅 🔗 ✍ 🛃 `X-Token` 🎚:
|
||||
|
||||
```Python hl_lines="1 4-6"
|
||||
```Python hl_lines="1 4-6" title="app/dependencies.py"
|
||||
{!../../../docs_src/bigger_applications/app/dependencies.py!}
|
||||
```
|
||||
|
||||
|
|
@ -143,7 +143,7 @@
|
|||
|
||||
, ↩️ ❎ 🌐 👈 🔠 *➡ 🛠️*, 👥 💪 🚮 ⚫️ `APIRouter`.
|
||||
|
||||
```Python hl_lines="5-10 16 21"
|
||||
```Python hl_lines="5-10 16 21" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -195,7 +195,7 @@ async def read_item(item_id: str):
|
|||
|
||||
👥 ⚙️ ⚖ 🗄 ⏮️ `..` 🔗:
|
||||
|
||||
```Python hl_lines="3"
|
||||
```Python hl_lines="3" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -265,7 +265,7 @@ that 🔜 ⛓:
|
|||
|
||||
✋️ 👥 💪 🚮 _🌅_ `tags` 👈 🔜 ✔ 🎯 *➡ 🛠️*, & ➕ `responses` 🎯 👈 *➡ 🛠️*:
|
||||
|
||||
```Python hl_lines="30-31"
|
||||
```Python hl_lines="30-31" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -290,7 +290,7 @@ that 🔜 ⛓:
|
|||
|
||||
& 👥 💪 📣 [🌐 🔗](dependencies/global-dependencies.md){.internal-link target=_blank} 👈 🔜 🌀 ⏮️ 🔗 🔠 `APIRouter`:
|
||||
|
||||
```Python hl_lines="1 3 7"
|
||||
```Python hl_lines="1 3 7" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -298,7 +298,7 @@ that 🔜 ⛓:
|
|||
|
||||
🔜 👥 🗄 🎏 🔁 👈 ✔️ `APIRouter`Ⓜ:
|
||||
|
||||
```Python hl_lines="5"
|
||||
```Python hl_lines="5" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -360,7 +360,7 @@ from .routers.users import router
|
|||
|
||||
, 💪 ⚙️ 👯♂️ 👫 🎏 📁, 👥 🗄 🔁 🔗:
|
||||
|
||||
```Python hl_lines="4"
|
||||
```Python hl_lines="5" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -368,7 +368,7 @@ from .routers.users import router
|
|||
|
||||
🔜, ➡️ 🔌 `router`Ⓜ ⚪️➡️ 🔁 `users` & `items`:
|
||||
|
||||
```Python hl_lines="10-11"
|
||||
```Python hl_lines="10-11" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -401,7 +401,7 @@ from .routers.users import router
|
|||
|
||||
👉 🖼 ⚫️ 🔜 💎 🙅. ✋️ ➡️ 💬 👈 ↩️ ⚫️ 💰 ⏮️ 🎏 🏗 🏢, 👥 🚫🔜 🔀 ⚫️ & 🚮 `prefix`, `dependencies`, `tags`, ♒️. 🔗 `APIRouter`:
|
||||
|
||||
```Python hl_lines="3"
|
||||
```Python hl_lines="3" title="app/internal/admin.py"
|
||||
{!../../../docs_src/bigger_applications/app/internal/admin.py!}
|
||||
```
|
||||
|
||||
|
|
@ -409,7 +409,7 @@ from .routers.users import router
|
|||
|
||||
👥 💪 📣 🌐 👈 🍵 ✔️ 🔀 ⏮️ `APIRouter` 🚶♀️ 👈 🔢 `app.include_router()`:
|
||||
|
||||
```Python hl_lines="14-17"
|
||||
```Python hl_lines="14-17" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -432,7 +432,7 @@ from .routers.users import router
|
|||
|
||||
📥 👥 ⚫️... 🎦 👈 👥 💪 🤷:
|
||||
|
||||
```Python hl_lines="21-23"
|
||||
```Python hl_lines="21-23" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ You can create the *path operations* for that module using `APIRouter`.
|
|||
|
||||
You import it and create an "instance" the same way you would with the class `FastAPI`:
|
||||
|
||||
```Python hl_lines="1 3"
|
||||
```Python hl_lines="1 3" title="app/routers/users.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/users.py!}
|
||||
```
|
||||
|
||||
|
|
@ -89,7 +89,7 @@ And then you use it to declare your *path operations*.
|
|||
|
||||
Use it the same way you would use the `FastAPI` class:
|
||||
|
||||
```Python hl_lines="6 11 16"
|
||||
```Python hl_lines="6 11 16" title="app/routers/users.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/users.py!}
|
||||
```
|
||||
|
||||
|
|
@ -114,13 +114,13 @@ We will now use a simple dependency to read a custom `X-Token` header:
|
|||
|
||||
=== "Python 3.9+"
|
||||
|
||||
```Python hl_lines="3 6-8"
|
||||
```Python hl_lines="3 6-8" title="app/dependencies.py"
|
||||
{!> ../../../docs_src/bigger_applications/app_an_py39/dependencies.py!}
|
||||
```
|
||||
|
||||
=== "Python 3.8+"
|
||||
|
||||
```Python hl_lines="1 5-7"
|
||||
```Python hl_lines="1 5-7" title="app/dependencies.py"
|
||||
{!> ../../../docs_src/bigger_applications/app_an/dependencies.py!}
|
||||
```
|
||||
|
||||
|
|
@ -129,7 +129,7 @@ We will now use a simple dependency to read a custom `X-Token` header:
|
|||
!!! tip
|
||||
Prefer to use the `Annotated` version if possible.
|
||||
|
||||
```Python hl_lines="1 4-6"
|
||||
```Python hl_lines="1 4-6" title="app/dependencies.py"
|
||||
{!> ../../../docs_src/bigger_applications/app/dependencies.py!}
|
||||
```
|
||||
|
||||
|
|
@ -160,7 +160,7 @@ We know all the *path operations* in this module have the same:
|
|||
|
||||
So, instead of adding all that to each *path operation*, we can add it to the `APIRouter`.
|
||||
|
||||
```Python hl_lines="5-10 16 21"
|
||||
```Python hl_lines="5-10 16 21" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -212,7 +212,7 @@ And we need to get the dependency function from the module `app.dependencies`, t
|
|||
|
||||
So we use a relative import with `..` for the dependencies:
|
||||
|
||||
```Python hl_lines="3"
|
||||
```Python hl_lines="3" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -282,7 +282,7 @@ We are not adding the prefix `/items` nor the `tags=["items"]` to each *path ope
|
|||
|
||||
But we can still add _more_ `tags` that will be applied to a specific *path operation*, and also some extra `responses` specific to that *path operation*:
|
||||
|
||||
```Python hl_lines="30-31"
|
||||
```Python hl_lines="30-31" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -307,7 +307,7 @@ You import and create a `FastAPI` class as normally.
|
|||
|
||||
And we can even declare [global dependencies](dependencies/global-dependencies.md){.internal-link target=_blank} that will be combined with the dependencies for each `APIRouter`:
|
||||
|
||||
```Python hl_lines="1 3 7"
|
||||
```Python hl_lines="1 3 7" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -315,7 +315,7 @@ And we can even declare [global dependencies](dependencies/global-dependencies.m
|
|||
|
||||
Now we import the other submodules that have `APIRouter`s:
|
||||
|
||||
```Python hl_lines="5"
|
||||
```Python hl_lines="5" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -377,7 +377,7 @@ The `router` from `users` would overwrite the one from `items` and we wouldn't b
|
|||
|
||||
So, to be able to use both of them in the same file, we import the submodules directly:
|
||||
|
||||
```Python hl_lines="5"
|
||||
```Python hl_lines="5" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -385,7 +385,7 @@ So, to be able to use both of them in the same file, we import the submodules di
|
|||
|
||||
Now, let's include the `router`s from the submodules `users` and `items`:
|
||||
|
||||
```Python hl_lines="10-11"
|
||||
```Python hl_lines="10-11" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -418,7 +418,7 @@ It contains an `APIRouter` with some admin *path operations* that your organizat
|
|||
|
||||
For this example it will be super simple. But let's say that because it is shared with other projects in the organization, we cannot modify it and add a `prefix`, `dependencies`, `tags`, etc. directly to the `APIRouter`:
|
||||
|
||||
```Python hl_lines="3"
|
||||
```Python hl_lines="3" title="app/internal/admin.py"
|
||||
{!../../../docs_src/bigger_applications/app/internal/admin.py!}
|
||||
```
|
||||
|
||||
|
|
@ -426,7 +426,7 @@ But we still want to set a custom `prefix` when including the `APIRouter` so tha
|
|||
|
||||
We can declare all that without having to modify the original `APIRouter` by passing those parameters to `app.include_router()`:
|
||||
|
||||
```Python hl_lines="14-17"
|
||||
```Python hl_lines="14-17" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -449,7 +449,7 @@ We can also add *path operations* directly to the `FastAPI` app.
|
|||
|
||||
Here we do it... just to show that we can 🤷:
|
||||
|
||||
```Python hl_lines="21-23"
|
||||
```Python hl_lines="21-23" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@
|
|||
|
||||
你可以导入它并通过与 `FastAPI` 类相同的方式创建一个「实例」:
|
||||
|
||||
```Python hl_lines="1 3"
|
||||
```Python hl_lines="1 3" title="app/routers/users.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/users.py!}
|
||||
```
|
||||
|
||||
|
|
@ -89,7 +89,7 @@
|
|||
|
||||
使用方式与 `FastAPI` 类相同:
|
||||
|
||||
```Python hl_lines="6 11 16"
|
||||
```Python hl_lines="6 11 16" title="app/routers/users.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/users.py!}
|
||||
```
|
||||
|
||||
|
|
@ -112,7 +112,7 @@
|
|||
|
||||
现在我们将使用一个简单的依赖项来读取一个自定义的 `X-Token` 请求首部:
|
||||
|
||||
```Python hl_lines="1 4-6"
|
||||
```Python hl_lines="1 4-6" title="app/dependencies.py"
|
||||
{!../../../docs_src/bigger_applications/app/dependencies.py!}
|
||||
```
|
||||
|
||||
|
|
@ -143,7 +143,7 @@
|
|||
|
||||
因此,我们可以将其添加到 `APIRouter` 中,而不是将其添加到每个路径操作中。
|
||||
|
||||
```Python hl_lines="5-10 16 21"
|
||||
```Python hl_lines="5-10 16 21" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -195,7 +195,7 @@ async def read_item(item_id: str):
|
|||
|
||||
因此,我们通过 `..` 对依赖项使用了相对导入:
|
||||
|
||||
```Python hl_lines="3"
|
||||
```Python hl_lines="3" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -265,7 +265,7 @@ from ...dependencies import get_token_header
|
|||
|
||||
但是我们仍然可以添加*更多*将会应用于特定的*路径操作*的 `tags`,以及一些特定于该*路径操作*的额外 `responses`:
|
||||
|
||||
```Python hl_lines="30-31"
|
||||
```Python hl_lines="30-31" title="app/routers/items.py"
|
||||
{!../../../docs_src/bigger_applications/app/routers/items.py!}
|
||||
```
|
||||
|
||||
|
|
@ -290,7 +290,7 @@ from ...dependencies import get_token_header
|
|||
|
||||
我们甚至可以声明[全局依赖项](dependencies/global-dependencies.md){.internal-link target=_blank},它会和每个 `APIRouter` 的依赖项组合在一起:
|
||||
|
||||
```Python hl_lines="1 3 7"
|
||||
```Python hl_lines="1 3 7" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -298,7 +298,7 @@ from ...dependencies import get_token_header
|
|||
|
||||
现在,我们导入具有 `APIRouter` 的其他子模块:
|
||||
|
||||
```Python hl_lines="5"
|
||||
```Python hl_lines="5" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -360,7 +360,7 @@ from .routers.users import router
|
|||
|
||||
因此,为了能够在同一个文件中使用它们,我们直接导入子模块:
|
||||
|
||||
```Python hl_lines="4"
|
||||
```Python hl_lines="5" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -368,7 +368,7 @@ from .routers.users import router
|
|||
|
||||
现在,让我们来包含来自 `users` 和 `items` 子模块的 `router`。
|
||||
|
||||
```Python hl_lines="10-11"
|
||||
```Python hl_lines="10-11" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -401,7 +401,7 @@ from .routers.users import router
|
|||
|
||||
对于此示例,它将非常简单。但是假设由于它是与组织中的其他项目所共享的,因此我们无法对其进行修改,以及直接在 `APIRouter` 中添加 `prefix`、`dependencies`、`tags` 等:
|
||||
|
||||
```Python hl_lines="3"
|
||||
```Python hl_lines="3" title="app/internal/admin.py"
|
||||
{!../../../docs_src/bigger_applications/app/internal/admin.py!}
|
||||
```
|
||||
|
||||
|
|
@ -409,7 +409,7 @@ from .routers.users import router
|
|||
|
||||
我们可以通过将这些参数传递给 `app.include_router()` 来完成所有的声明,而不必修改原始的 `APIRouter`:
|
||||
|
||||
```Python hl_lines="14-17"
|
||||
```Python hl_lines="14-17" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
@ -432,7 +432,7 @@ from .routers.users import router
|
|||
|
||||
这里我们这样做了...只是为了表明我们可以做到🤷:
|
||||
|
||||
```Python hl_lines="21-23"
|
||||
```Python hl_lines="21-23" title="app/main.py"
|
||||
{!../../../docs_src/bigger_applications/app/main.py!}
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue