首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >FastAPI后台开发基础(20): 设置 FastAPI 元数据

FastAPI后台开发基础(20): 设置 FastAPI 元数据

原创
作者头像
密码学人CipherHUB
发布2024-11-20 12:54:58
发布2024-11-20 12:54:58
3570
举报
文章被收录于专栏:编码视界编码视界

设置描述信息

代码语言:python
复制
from __future__ import annotations

import uuid

import uvicorn
from fastapi import FastAPI

description = """
这是我的FastAPI描述信息 🚀😄

# 一级标题

这是一级标题下的 **内容**.

## 这是二级标题

description 参数中可以使用 markdown 语法,比如设置列表:

* **Create users** (_not implemented_).
* **Read users** (_not implemented_).
"""

app = FastAPI(
    title = "FastAPI Metadata 元数据设置",
    description = description,
    summary = "总结信息",
    version = "0.0.1",
    terms_of_service = "https://cloud.tencent.com/developer/user/1371154",
    contact = {
        "name": "bowenerchen",
        "url": "https://cloud.tencent.com/developer/user/1371154",
        # "email": "bowenerchen@demotest.com",
    },
    license_info = {
        "name": "Apache 2.0",
        # "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
        "identifier": "MIT"
    },
)


@app.get("/items/")
async def read_items():
    return [{"unique_id": uuid.uuid4().hex}]


if __name__ == '__main__':
    uvicorn.run(app, host = '127.0.0.1', port = 18081)
效果展示
效果展示

设置接口文档与文档 URL

代码语言:python
复制
from __future__ import annotations

import uuid

import uvicorn
from fastapi import FastAPI

path_operation_tags = [
    {
        "name": "标签 1",
        "description": "Manage items. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Items external docs",
            "url": "https://spdx.org/licenses/MIT.html",
        },
    },
    {
        "name": "标签 2",
        "description": "Manage users. So _fancy_ they have their own docs.",
        "externalDocs": {
            "description": "Users external docs",
            "url": "https://www.apache.org/licenses/LICENSE-2.0.html",
        },
    },
]
app = FastAPI(openapi_tags = path_operation_tags,
              openapi_url = '/my_custom_openapi_json',
              docs_url = '/my_custom_swagger_ui_html',
              redoc_url = '/my_custom_redoc_html')


@app.get("/items_1/", tags = ['标签 1'])
async def read_items_1():
    return [{"unique_id": uuid.uuid4().hex}]


@app.get("/items_2/", tags = ['标签 2'])
async def read_items_2():
    return [{"unique_id": uuid.uuid4().hex}]


if __name__ == '__main__':
    uvicorn.run(app, host = '127.0.0.1', port = 18081)
运行效果
运行效果
openapi查看效果
openapi查看效果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 设置描述信息
  • 设置接口文档与文档 URL
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档