2022-5-19 fastapi突然无法打开docs了

网上查了很多都是升级什么的。

我根据这篇文档修改好了:https://www.icode9.com/content-4-1383591.html

原因就是https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui-bundle.js 访问不通了,我还以为是内网限制了,自己用手机网络依据无法访问~

那我们就修改地址他的访问地址,我没有深入了解,根据上面办法解决了

 

我这里直接写linux修改文件的方法:

uos需要修改的文件路径在:/usr/local/lib/python3.7/dist-packages/fastapi/openapi/docs.py

其他的系统:find /  -name docs.py

 

    # swagger_js_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui-bundle.js",
    swagger_js_url: str = "https://petstore.swagger.io/swagger-ui-bundle.js",
    # swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@4/swagger-ui.css",
    swagger_css_url: str = "https://petstore.swagger.io/swagger-ui.css",
    # swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
    swagger_favicon_url: str = "https://petstore.swagger.io/favicon-32*32.png",

 

 

下面有一位仁兄有更好的办法:

复制代码
from fastapi import FastAPI
from fastapi.openapi.docs import (
    get_redoc_html,
    get_swagger_ui_html,
    get_swagger_ui_oauth2_redirect_html,
)
from fastapi.staticfiles import StaticFiles

app = FastAPI(docs_url=None, redoc_url=None)

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",
    )
复制代码

 

posted @   Tarzen  阅读(1277)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-05-19 word中插入代码不好看,试试highlightcode
2020-05-19 linux 命令关闭网络
2020-05-19 python调用linux命令-但是需要sudo权限
2019-05-19 Python3.5-20190519-廖老师-自我笔记-面向对象中slots变量--@property的使用
2019-05-19 Python3.5-20190519-廖老师-自我笔记-获取对象信息
点击右上角即可分享
微信分享提示