当前位置: 首页 > news >正文

【Python】Fastapi swagger-ui.css 、swagger-ui-bundle.js 无法加载,docs无法加载,redocs无法使用

使用fastapi的时候,swagger-ui.css 、swagger-ui-bundle.js、redoc.standalone.js 有时候无法加载(国内环境原因或者是局域网屏蔽),此时就需要自己用魔法下载好对应文件,然后替换到fastapi里面去。

fastapi里面依靠这2个函数实现docs和redoc:

from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html

fastapi里面官网给的解决办法:

https://github.com/tiangolo/fastapi/blob/ac93277d3b506f4076aee1af6ae5a86406f545c6/docs_src/custom_docs_ui/tutorial001.py#L4

实践起来就是,首先下载好这些文件:
在这里插入图片描述

其次复现出/docs路由和/redoc路由:

import os
import uvicorn
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_htmlapp = FastAPI(title='outpainting_captioning_upscaler',description='outpainting images, captioning images,upscaler images',version='1.0.0',docs_url=None,redoc_url=None,  # 设置 ReDoc 文档的路径)
app.add_middleware(CORSMiddleware,allow_origins=["*"],allow_credentials=True,allow_methods=["*"],allow_headers=["*"],
)
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="/openapi.json",title="xx",# oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url,swagger_js_url='/static/swagger/swagger-ui-bundle.js',swagger_css_url='/static/swagger/swagger-ui.css',swagger_favicon_url='/static/swagger/img.png',)@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/swagger/redoc.standalone.js",)# 写个测试接口
@app.get('/xx')
async def test():return {'message': 'test success'}if __name__ == '__main__':uvicorn.run(f'{os.path.basename(__file__).split(".")[0]}:app',host='0.0.0.0',port=7777,reload=False,workers=1)

其中文件也可以去这里下载:

下载:https://wwsm.lanzouy.com/i682Q1frjnmd 密码:9zw2

成功:
在这里插入图片描述

http://www.lryc.cn/news/242383.html

相关文章:

  • 算法-中等-链表-两数相加
  • STC单片机选择外部晶振烧录程序无法切换回内部晶振导致单片机不能使用
  • 使用STM32+SPI Flash模拟U盘
  • 【自主探索】基于 frontier_exploration 的单个机器人自主探索建图
  • 模板初阶(1):函数模板,类模板
  • AIGC: 关于ChatGPT中生成输出表格/表情/图片/图表这些非文本的方式
  • gen_arrow_contour_xld
  • 智能时代的智能工具(gpt)国产化助手
  • 量子计算 | 解密著名量子算法Shor算法和Grover算法
  • 缓存组件状态,提升用户体验:探索 keep-alive 的神奇世界
  • 万字长文 - Python 日志记录器logging 百科全书 - 高级配置之 日志文件配置
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • OpenGL 绘制旋转球(Qt)
  • 解决:javax.websocket.server.ServerContainer not available 报错问题
  • 81基于matlab GUI的图像处理
  • 虚拟机系列:vmware和Oracle VM VirtualBox虚拟机的区别,简述哪一个更适合我?以及相互转换
  • Go lumberjack 日志轮换和管理
  • git常用命令(git github ssh)
  • 完美解决:Nginx访问PHP出现File not found.
  • 音视频5、libavformat-2
  • python opencv -模板匹配
  • 大数据技能大赛(高职组)答案
  • C++动态规划算法:最多可以参加的会议数目
  • Windows 下安装MySQL8.0 Zip
  • 8.2 Windows驱动开发:内核解锁与强删文件
  • 【Spark源码分析】事件总线机制分析
  • c语言第七弹--扫雷小游戏!
  • 浏览器是什么
  • 一文彻底看懂Python切片,Python切片理解与操作
  • 聊聊tomcat的connection-timeout