参考官方文档地址https://fastapi.tiangolo.com/zh/howto/customdocsuiassets/#selfhosting-javascriptandcss-fordocs

版本介绍

python==3.9
fastapi==0.104.1

项目目录

.
├── app
│   ├── __init__.py
│   ├── main.py

改变后的项目目录

.
├── app
│   ├── __init__.py
│   ├── main.py
└── static
	├── redoc.standalone.js
	├── swagger-ui-bundle.js
	└── swagger-ui.css
三个文件下载地址分别为:
  1. https://cdn.jsdelivr.net/npm/swagger-uidist@5.9.0/swagger-uibundle.js
  2. https://cdn.jsdelivr.net/npm/swagger-uidist@5.9.0/swagger-ui.css
  3. https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js
    如无法下载,可参考项目https://github.com/silkjava/fastapitutorial

项目启动文件中,添加如下代码


# ========设置docs文档本地文件=========
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import (
    get_redoc_html,
    get_swagger_ui_html,
    get_swagger_ui_oauth2_redirect_html,
)
app.mount("/static", StaticFiles(directory="static"), name="static")

@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title=app.title + " - Swagger UI",
        oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,
        swagger_js_url="/static/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger-ui.css",
    )

@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def swagger_ui_redirect():
    return get_swagger_ui_oauth2_redirect_html()

@app.get("/redoc", include_in_schema=False)
async def redoc_html():
    return get_redoc_html(
        openapi_url=app.openapi_url,
        title=app.title + " - ReDoc",
        redoc_js_url="/static/redoc.standalone.js",
    )
# ========设置docs文档本地文件=========

原文地址:https://blog.csdn.net/silk_java/article/details/134785095

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_38958.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注