FastAPI 教程翻译 - 用户指南 32 - 元数据和文档网址
FastAPI Tutorial - User Guide - Metadata and Docs URLs
There are several things that you can configure in your FastAPI application.
您可以在 FastAPI 应用程序中配置几件事。
Title, description, and version
标题、描述和版本
You can set the:
您可以设置:
-
Title: used as your API’s title/name, in OpenAPI and the automatic API docs UIs.
标题:在 OpenAPI 和自动 API 文档用户界面中用作 API 的标题 / 名称。
-
Description: the description of your API, in OpenAPI and the automatic API docs UIs.
说明:在 OpenAPI 和自动 API 文档用户界面中的 API 说明。
-
Version: the version of your API, e.g.
v2
or2.5.0
.版本:您的 API 版本,例如:
v2
或2.5.0
。-
Useful for example if you had a previous version of the application, also using OpenAPI.
例如,如果您具有该应用程序的先前版本(也使用 OpenAPI),则很有用。
-
To set them, use the parameters title
, description
, and version
:
要设置它们,请使用参数 title
、description
和 version
:
from fastapi import FastAPI
app = FastAPI(
title="My Super Project",
description="This is a very fancy project, with auto docs for the API and everything",
version="2.5.0",
)
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
With this configuration, the automatic API docs would look like:
使用此配置,自动 API 文档将如下所示:
OpenAPI URL
By default, the OpenAPI schema is served at /openapi.json
.
默认情况下,OpenAPI 架构位于 /openapi.json
。
But you can configure it with the parameter openapi_url
.
但是您可以使用参数 openapi_url
对其进行配置。
For example, to set it to be served at /api/v1/openapi.json
:
例如,将其设置为在 /api/v1/openapi.json
中投放:
from fastapi import FastAPI
app = FastAPI(openapi_url="/api/v1/openapi.json")
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
If you want to disable the OpenAPI schema completely you can set openapi_url=None
.
如果要完全禁用 OpenAPI 模式,则可以设置 openapi_url=None
。
Docs URLs
文档网址
You can configure the two documentation user interfaces included:
您可以配置包括的两个文档用户界面:
-
Swagger UI: served at
/docs
.Swagger UI:在
/docs
中提供。-
You can set its URL with the parameter
docs_url
.您可以使用参数
docs_url
设置其网址。 -
You can disable it by setting
docs_url=None
.您可以通过设置
docs_url=None
禁用它。
-
-
ReDoc: served at
/redoc
.ReDoc:在
/redoc
中提供。-
You can set its URL with the parameter
redoc_url
.您可以使用参数
redoc_url
设置其网址。 -
You can disable it by setting
redoc_url=None
.您可以通过设置
redoc_url=None
来禁用它。
-
For example, to set Swagger UI to be served at /documentation
and disable ReDoc:
例如,要将 Swagger UI 设置为可在 /documentation
服务,并禁用 ReDoc:
from fastapi import FastAPI
app = FastAPI(docs_url="/documentation", redoc_url=None)
@app.get("/items/")
async def read_items():
return [{"name": "Foo"}]
API(docs_url="/documentation", redoc_url=None)
@app.get("/items/")
async def read_items():
return [{“name”: “Foo”}]