建网站的步骤和方法,智慧团建官方登录,手机网站开发介绍,2023免费推广网站环境#xff1a;openEuler、python 3.11.6、fastapi 0.115.2
背景#xff1a;居家办公#xff0c;默认搭建的fastapi的docs接口为空白
时间#xff1a;20241016
说明#xff1a;网上很多教程的缺点是复杂#xff08;但是能够了解的更清楚#xff09;#xff0c;使用…环境openEuler、python 3.11.6、fastapi 0.115.2
背景居家办公默认搭建的fastapi的docs接口为空白
时间20241016
说明网上很多教程的缺点是复杂但是能够了解的更清楚使用官方文档解决很便利
官方文档地址Custom Docs UI Static Assets 1、搭建环境 安装相应的python包
pip install fastapi uvicorn
创建main文件
# main.py
from fastapi import FastAPI
app FastAPI()app.get(/) # 根路由
async def root():return I want to change the worldif __name__ __main__:import uvicornuvicorn.run(app, host0.0.0.0, port8000)# 启动命令uvicorn main:app --reload --host 0.0.0.0 --port 8000
运行测试
说明启动成功
(venv) [jackLaptop-L14-gen4 fastTest]$ uvicorn main:app --reload --host 0.0.0.0 --port 8000
INFO: Will watch for changes in these directories: [/home/jack/fastTest]
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRLC to quit)
INFO: Started reloader process [6119] using StatReload
INFO: Started server process [6121]
INFO: Waiting for application startup.
INFO: Application startup complete. 浏览器查看 至此说明基本环境搭建完毕
2、问题发现
默认访问 http://172.26.20.199:8000/docs 应该如下 可问题却是空白 原因分析
fastapi的文件是提供的CDN为国外的网址偶尔可能存在网络延迟导致为空白使用F12可以看到是文件获取不到
3、解决问题
第一种方式的再试试存在这种可能性
第二种方式为复制官方文档提供的部分代码
# 增加的代码部分from fastapi.openapi.docs import (get_redoc_html,get_swagger_ui_html,get_swagger_ui_oauth2_redirect_html,
)app FastAPI(docs_urlNone, redoc_urlNone)app.get(/docs, include_in_schemaFalse)
async def custom_swagger_ui_html():return get_swagger_ui_html(openapi_urlapp.openapi_url,titleapp.title - Swagger UI,oauth2_redirect_urlapp.swagger_ui_oauth2_redirect_url,swagger_js_urlhttps://unpkg.com/swagger-ui-dist5/swagger-ui-bundle.js,swagger_css_urlhttps://unpkg.com/swagger-ui-dist5/swagger-ui.css,)
上述代码完全无脑复制到你的main.py中覆盖app 这一行即可复制完如下
# main.py
from fastapi import FastAPIfrom fastapi.openapi.docs import (get_redoc_html,get_swagger_ui_html,get_swagger_ui_oauth2_redirect_html,
)app FastAPI(docs_urlNone, redoc_urlNone)app.get(/docs, include_in_schemaFalse)
async def custom_swagger_ui_html():return get_swagger_ui_html(openapi_urlapp.openapi_url,titleapp.title - Swagger UI,oauth2_redirect_urlapp.swagger_ui_oauth2_redirect_url,swagger_js_urlhttps://unpkg.com/swagger-ui-dist5/swagger-ui-bundle.js,swagger_css_urlhttps://unpkg.com/swagger-ui-dist5/swagger-ui.css,)app.get(/) # 根路由
async def root():return I want to change the worldif __name__ __main__:import uvicornuvicorn.run(app, host0.0.0.0, port8000)# 启动命令uvicorn main:app --reload --host 0.0.0.0 --port 8000
保存fastapi会自动重新加载刷新网页即可。 文章转载自: http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.mspqw.cn.gov.cn.mspqw.cn http://www.morning.fgsqz.cn.gov.cn.fgsqz.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.sthp.cn.gov.cn.sthp.cn http://www.morning.mqfhy.cn.gov.cn.mqfhy.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.rlcqx.cn.gov.cn.rlcqx.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.ycmpk.cn.gov.cn.ycmpk.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.ppzgr.cn.gov.cn.ppzgr.cn http://www.morning.hmbxd.cn.gov.cn.hmbxd.cn http://www.morning.wlqbr.cn.gov.cn.wlqbr.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.ykklw.cn.gov.cn.ykklw.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.tpnx.cn.gov.cn.tpnx.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.kmqms.cn.gov.cn.kmqms.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.cbvlus.cn.gov.cn.cbvlus.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.bojkosvit.com.gov.cn.bojkosvit.com http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.jzfrl.cn.gov.cn.jzfrl.cn http://www.morning.mzgq.cn.gov.cn.mzgq.cn http://www.morning.hrydl.cn.gov.cn.hrydl.cn http://www.morning.zrpbf.cn.gov.cn.zrpbf.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.jhxdj.cn.gov.cn.jhxdj.cn http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.trjp.cn.gov.cn.trjp.cn http://www.morning.jqpq.cn.gov.cn.jqpq.cn http://www.morning.cnkrd.cn.gov.cn.cnkrd.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.nfbxgtj.com.gov.cn.nfbxgtj.com http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.qzxb.cn.gov.cn.qzxb.cn http://www.morning.pxbrg.cn.gov.cn.pxbrg.cn http://www.morning.lpyjq.cn.gov.cn.lpyjq.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.jkftn.cn.gov.cn.jkftn.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.rwwdp.cn.gov.cn.rwwdp.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.tmcmj.cn.gov.cn.tmcmj.cn http://www.morning.wctqc.cn.gov.cn.wctqc.cn http://www.morning.smzr.cn.gov.cn.smzr.cn http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn