Flask-RESTful提供了一个名为flask-redoc的扩展,可以将生成的API文档转换为Redoc UI。 首先,我们需要安装flask-redoc扩展:pip install flask-redoc然后,我们需要使用flask_redoc的Redoc类来配置Redoc UI。 = Redoc(app, title='API Documentation', openapi_url='http://localhost:5000/api/spec')if __name__ == '__main__': app.run(debug=True)在上面的例子中,我们导入了flask_redoc的Redoc类,并创建了一个redoc对象。 然后,在程序运行时,我们可以通过访问http://localhost:5000/redoc/来查看生成的Redoc UI。
ReDoc:服务于 /redoc。 可以使用参数 redoc_url 设置它的 URL。 可以通过设置 redoc_url=None 禁用它。 我们一直没有看过redoc,我们今天看下 我们重新定义下对应的文档的地址 app = FastAPI( openapi_tags=tags_metadata, docs_url ="/openapi", redoc_url="/apidoc" ) 我们启动后看下。
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), # ReDoc 页面 path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), # ...]查看效果启动 Django 项目后,通过以下地址访问文档:Swagger UI 界面:http://localhost:8000/swagger/ReDoc 界面:http://localhost:8000/redoc api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), # ReDoc 界面 path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'), #
app = FastAPI(openapi_url=None) 这样会导致 Swagger API 文档也无法访问 两个文档 URL docs_url: Optional[str] = "/docs", redoc_url : Optional[str] = "/redoc", Swagger API 默认 /docs 使用参数 docs_url 设置其 URL 也可以通过设置 docs_url=None 来禁用它 ReDoc 默认 /redoc 使用参数 redoc_url 设置其 URL 也可以通过设置 redoc_url=None 来禁用它 实际代码 from fastapi import FastAPI app = FastAPI(docs_url="/documentation", redoc_url="/redo") @app.get("/items/") async def read_items():
2.ReDoc 访问路径:默认情况下,可以通过访问 http://127.0.0.1:8000/redoc 来查看 ReDoc 风格的文档。 特点:ReDoc 是另一种交互式文档界面,具有清晰简洁的外观。它使得开发者能够以可读性强的方式查看 API 的描述、请求和响应。 与 Swagger UI 不同,ReDoc 的设计强调文档的可视化和用户体验。 自动生成文档:内置 Swagger UI 和 ReDoc 两种风格的交互式 API 文档,方便开发者测试和调试。
打开http://127.0.0.1:8000/docs就能看到自动生成的交互式 API 文档(由 Swagger UI 提供): 可选的API文档 前往http://127.0.0.1:8000/redoc 就能看到可选的自动生成文档 (由 ReDoc 提供): 源码解析 from fastapi import FastAPI 导入fastapi的包 app = FastAPI() 创建一个FastAPI default_response_class: Type[Response] = Default(JSONResponse), docs_url: Optional[str] = "/docs", redoc_url : Optional[str] = "/redoc", swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect title='FastAPI Study', description='FastAPI教程', version='1.0.0', docs_url='/docs', redoc_url
图片或者你可以在创建 APIFlask 实例时通过 docs_ui 参数来设置 API 文档 UI (APIFlask 1.1+):app = APIFlask(__name__, docs_ui='redoc ://cdnjs.cloudflare.com/ajax/libs/swagger-ui/4.11.1/swagger-ui-standalone-preset.min.js'app.config['REDOC_STANDALONE_JS '] = 'https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js'自动生成的 OpenAPI 规范文件可以在 http://localhost
你试一试把/docs改成/redoc看看: 要关闭 redoc也可以使用相同的方法: app = FastAPI(docs_url=None, redoc_url=None) 运行效果如下图所示: 关于这两个参数的更多详情 = 'develop': app = FastAPI(docs_url=None, redoc_url=None) else: app = FastAPI() 当我们在自己电脑上开发的时候
path('swagger/', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger'), path('redoc /', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc') ] 访问: http://127.0.0.1:8000/swagger.json 访问 http://127.0.0.1:8000/redoc/ ? 开始项目 创建项目 ?
Node.js / Go) 基于 Starlette(异步 Web 框架) + Pydantic(高性能数据校验) Python Web 框架中性能天花板之一 ✔ 自动生成 API 文档:Swagger + ReDoc 无需编写文档,自动生成: /docs(Swagger UI) /redoc(Redoc) ✔ 使用 Python 类型提示自动校验参数 自动请求参数校验 自动响应模型校验 自动补全(IDE 体验极佳) app --reload 浏览器访问: http://127.0.0.1:8000/hello http://127.0.0.1:8000/docs http://127.0.0.1:8000/redoc 十、FastAPI 自动生成接口 API 文档 FastAPI 自动支持两个文档: 文档 地址 用途 Swagger UI /docs 在线调试 + 文档 ReDoc /redoc 更适合阅读的文档
path('swagger', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'), path('redoc /', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), ] 5. 验证效果 1、服务运行起来后,默认端口为8000,浏览器访问http://127.0.0.1:8000/redoc/,可查看redoc ui,效果如下所示。 ? PS: ReDoc 是另一个 Swagger UI 工具。 3、继续访问http://127.0.0.1:8000/swagger,即可看到日常我们熟悉的Swagger接口文档界面了。 ?
这里,我用了个人比较常用的docker镜像redoc为例,搭建一个在线接口文档平台。 该镜像更多的使用方式可参考:https://hub.docker.com/r/redocly/redoc/ 运行如下命令,即将swagger.json加载到镜像中: docker run --name 80:80 -v $(pwd)/gen/swagger.json:/usr/share/nginx/html/swagger.json -e SPEC_URL=swagger.json redocly/redoc 扩展点 - 公共的文档服务器: 我们往往更希望把文档放在一个公共的服务器上,可以简单地利用这两个关键技术实现: https://hub.docker.com/r/redocly/redoc/ 中的watch
/$', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'), ... ] drf-yasg会暴露4种默认路径 : /swagger.json, JSON 格式的 API 定义 /swagger.yaml, YAML 格式的 API 定义 /swagger/, 基于原生 swagger-ui 样式的前端页面 /redoc /, 基于 ReDoc 样式的前端页面 4.5 访问查看 完成后重启项目进行访问 swagger ? redoc ? 文档 SchemaView.with_ui(renderer, cache_timeout, cache_kwargs): 返回使用指定UI渲染器的视图函数, 可选的UI渲染器有: swagger, redoc
: 'SIDECAR', # shorthand to use the sidecar instead 'SWAGGER_UI_FAVICON_HREF': 'SIDECAR', 'REDOC_DIST swagger/ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), path('swagger/redoc /', SpectacularRedocView.as_view(url_name='schema'), name='redoc'), # YOUR PATTERNS ] 访问:http:/ swagger/ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), path('swagger/redoc /', SpectacularRedocView.as_view(url_name='schema'), name='redoc'), # My Router path('user/',
Django REST framework支持通过Swagger、ReDoc等工具生成API文档。1. 使用ReDoc生成文档ReDoc是另一种API文档生成工具,它提供了美观的静态文档页面。 在urls.py中添加ReDoc路由:# myproject/urls.pyfrom drf_yasg.views import get_schema_viewfrom drf_yasg import path('redoc/', schema_view.with_ui('redoc', cache_timeout=0), name='schema-redoc'),]通过访问/redoc/路径,您可以查看使用 ReDoc生成的静态文档。
三、redoc github地址:https://github.com/Redocly/redoc 开源协议:MIT License Star: 10.7K 开发语言:typescript、javascript redoc自己号称是一个最好的在线文档工具。它支持swagger接口数据,提供了多种生成文档的方式,非常容易部署。 使用redoc-cli能够将您的文档捆绑到零依赖的 HTML文件中,响应式三面板设计,具有菜单/滚动同步。
title', description='这是FastAPI教程的文档——description', version='1.0.0', docs_url='/docs', redoc_url ='/redoc', ) app.include_router(app01, prefix='/stu', tags=['路径参数与数值参数校验']) app.include_router(app02 title', description='这是FastAPI教程的文档——description', version='1.0.0', docs_url='/docs', redoc_url ='/redoc', ) 这个在项目开始中就提到了,这些元数据也都是设置文档的信息的,具体的效果参看下面的图片; 路径配置 @app.get("/hello/{name}", tags=["默认
同时强调APK中安全安卓实施的最佳做法 调查结果可以被编辑,假阳性可以被分流和删除 所有的扫描结果都可以导出为PDF格式 用户认证和用户管理 具有Swagger和ReDoc 有关详细信息,请参阅TLS 要停止并删除容器,请运行 docker-compose down API v1 REST API 与 Swagger 和 ReDoc 的集成 用法 用于身份验证和获取令牌的端点 规范的 JSON 视图位于 /swagger.json API 规范的 YAML 视图位于 /swagger.yaml API 规范的 swagger-ui 视图位于 /swagger/ API 规范的 ReDoc 视图位于 /redoc/ TLS 条件 将证书添加到 nginx/ssl 生成自签名证书: openssl req -x509 -nodes -days 1 -newkey rsa:4096 -subj
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
1、http://127.0.0.1:8000/docs (基于 Swagger UI) 2、http://127.0.0.1:8000/redoc (基于 ReDoc) 这两种方式都是非常的有利于我们做调试的