简体   繁体   English

使用 Flask 和 Swagger 公开 API 文档

[英]Exposing API Documentation Using Flask and Swagger

I have build a small service with flask and already wrote a swagger yaml file to describe it's API.我建立一个小型服务flask和已经写了一个招摇YAML文件来描述它的API。 How can I expose the swagger file through the flask app?如何通过flask应用程序公开swagger文件?

I didn't mean to expose the file itself ( send_from_directory ) but to create new endpoint that will show it as swagger-ui (interactive, if possible)我并不是要公开文件本身( send_from_directory ),而是要创建将其显示为 swagger-ui 的新端点(如果可能,是交互式的)

OK, this is what I did.好的,这就是我所做的。

I used flasgger to and wrap my app with flasgger.Swagger .我用flasgger并换我用的应用程序flasgger.Swagger than I added 2 endpoints:比我添加了 2 个端点:

  1. /_api serves the YAML file (with send_from_directory ) /_api提供 YAML 文件(使用send_from_directory
  2. /api redirects to the flasgger api /apidocs/index.html?url=/api I'm sure it can be done better, but I failed to find it. /api重定向到 flasgger api /apidocs/index.html?url=/api我确信它可以做得更好,但我没有找到它。

github: https://github.com/eplaut/python-butler/blob/master/butler/butler.py#L119 github: https : //github.com/eplaut/python-butler/blob/master/butler/butler.py#L119

您可以使用 Flasgger,这是一个为 Flask 包装 Swagger UI 的 Flask 扩展http://github.com/rochacbruno/flasgger

There are three ways of doing it:有以下三种方法:

  • via Restful-Api (Api.doc)通过 Restful-Api (Api.doc)
  • via getting swagger templates通过获取 swagger 模板
  • via registering blueprints (from flask-swagger-ui or smth).通过注册蓝图(来自flask-swagger-ui 或smth)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM