基于flask实现swagger在线api文档

  1. 引言:
    • Flask是一款流行的Python实现的Web开发微框架;
    • Swagger是一款Restful接口的文档在线自动生成+功能测试功能软件
  2. 支持Python+Flask的Swagger库不少,有flask-swag,flask-swagger,flasgger
  3. flask的debug模式会额外开启一个进程,这个进程负责监控代码是否发生变化,即DEBUG模式下flask多开一个线程来监视项目的变化,因此可以打开debug模式时,发生端口被占用,可以设置 use_reloader=False
  4. 通过包flask-swagger获取独立的docstring文件test.yaml,参考https://github.com/getsling/flask-swagger
  5. 通过包flask_swagger_ui添加Swagger UI的蓝图,参考https://github.com/sveint/flask-swagger-ui

python代码

from flask import Flask, jsonify
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint

app = Flask('Test')

# 1. 通过swagger_from_file关键词搜索对应的文档
@app.route('/fun_json')
def spec():   
   return jsonify(swagger(app, from_file_keyword="swagger_from_file"))

# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(
    base_url='/api/docs',  # URL for exposing Swagger UI (without trailing '/')
    api_url='http://127.0.0.1:5003/fun_json',  # Our API url (can of course be a local resource)
    config={  # Swagger UI config overrides
        'app_name': "测试应用",
        'title': "测试应用"
    },
)

# 2. swagger_from_file指定对应的配置文件,其中yaml文件的配置
@app.route('/def_fun', methods=["POST"])
def login():
   """
   swagger_from_file: test.yaml
   """
   return 'login success'

app.register_blueprint(swaggerui_blueprint)

if __name__ == '__main__':
   app.run(host='127.0.0.1', port='5003', debug=False, use_reloader=False)
   # 浏览器打开http://127.0.0.1:5003/api/docs/

yaml文件

Create a new user
---
tags:
  - users
definitions:
  - schema:
      id: Group
      properties:
        name:
          type: string
          description: the group's name
parameters:
  - in: body
    name: body
    schema:
      id: User
      required:
        - email
        - name
      properties:
        email:
          type: string
          description: email for user
        name:
          type: string
          description: name for user
        address:
          description: address for user
          schema:
            id: Address
            properties:
              street:
                type: string
              state:
                type: string
              country:
                type: string
              postalcode:
                type: string
        groups:
          type: array
          description: list of groups
          items:
            $ref: "#/definitions/Group"
responses:
  201:
    description: User created

页面显示

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flask Restful Swagger是一种用于构建RESTful API自动生成API文档的工具。 Flask是一个轻量级的Python Web框架,提供了构建Web应用程序所需的基本功能。而Flask Restful是Flask的扩展,它使得构建RESTful API更加简单和高效。 Swagger是一种用于描述和可视化RESTful API的工具,它使用JSON或YAML格式定义API的接口和参数,并生成交互式的API文档。 使用Flask Restful Swagger,我们可以在Flask框架中轻松创建和管理RESTful API,并自动生成API文档。通过定义API的路由、请求参数和响应结果,我们可以方便地生成具有完整描述和示例的API文档Flask Restful Swagger提供了一系列装饰器和注解,用于定义API的路由和参数。我们可以使用@api.route装饰器定义API的路由,并使用@api.doc装饰器定义API文档信息。通过注解和装饰器的结合,我们可以灵活地描述API的请求参数、响应结果以及其他相关信息。 在生成API文档时,Flask Restful Swagger会解析我们定义的API信息,并将其转换成Swagger所需的JSON或YAML格式。然后,Swagger将根据这些信息生成交互式的API文档,其中包含API的路由、请求方式、请求参数、响应结果等详细信息。这使得我们可以方便地查看和测试API的接口,并清楚地理解API的使用方法和功能。 总结来说,通过使用Flask Restful Swagger,我们可以在Flask框架中快速构建和管理RESTful API,并自动生成可交互的API文档。这不仅提高了API的开发效率,也方便了API的使用者理解和测试API的接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值