4.Flasgger-接口文档化

一.下载安装

pip install  flasgger==0.9.7.1

二.基本使用

from flask import Flask, jsonify
from flasgger import Swagger

app = Flask(__name__)
swagger = Swagger(app)

@app.route('/colors/<palette>/')
def colors(palette):
    """Example endpoint returning a list of colors by palette
    This is using docstrings for specifications.
    ---
    parameters:
      - name: palette
        in: path
        type: string
        enum: ['all', 'rgb', 'cmyk']
        required: true
        default: all
    definitions:
      Palette:
        type: object
        properties:
          palette_name:
            type: array
            items:
              $ref: '#/definitions/Color'
      Color:
        type: string
    responses:
      200:
        description: A list of colors (may be filtered by palette)
        schema:
          $ref: '#/definitions/Palette'
        examples:
          rgb: ['red', 'green', 'blue']
    """
    all_colors = {
        'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
        'rgb': ['red', 'green', 'blue']
    }
    if palette == 'all':
        result = all_colors
    else:
        result = {palette: all_colors.get(palette)}

    return jsonify(result)

app.run(debug=True)

三.打开文档

http://127.0.0.1:5000/apidocs
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python 可以使用第三方库 `flasgger` 来实现接口文档的自动生成和下载功能。`flasgger` 通过解析 API 的注释来自动生成接口文档,并提供了下载文档的功能。 安装 `flasgger`: ``` pip install flasgger ``` 在 Flask 应用中,使用 `flasgger` 的 `Swagger` 类来配置接口文档: ```python from flask import Flask from flasgger import Swagger app = Flask(__name__) swagger = Swagger(app) @app.route('/hello') def hello(): """ This is an example endpoint that returns a greeting. --- responses: 200: description: A greeting message """ return 'Hello, World!' if __name__ == '__main__': app.run(debug=True) ``` 在上面的例子中,`hello` 函数的注释被解析为接口文档中的描述。`responses` 字段指定了响应的格式。运行应用后,在浏览器中访问 `http://localhost:5000/apidocs/` 就可以查看接口文档了。 要实现接口文档的下载功能,可以在应用中添加一个路由,在该路由中使用 `flasgger` 的 `swag_from` 装饰器来加载接口文档,并将其保存为文件: ```python from flask import Flask, send_file from flasgger import Swagger, swag_from app = Flask(__name__) swagger = Swagger(app) @app.route('/download') @swag_from('swagger.yaml') def download(): """ Download the API documentation in YAML format. """ with open('swagger.yaml', 'r') as f: data = f.read() return send_file(data, attachment_filename='swagger.yaml', as_attachment=True) @app.route('/hello') def hello(): """ This is an example endpoint that returns a greeting. --- responses: 200: description: A greeting message """ return 'Hello, World!' if __name__ == '__main__': app.run(debug=True) ``` 在上面的例子中,`download` 函数使用 `swag_from` 装饰器来加载接口文档。`send_file` 函数将文件作为响应发送给客户端。`attachment_filename` 参数指定下载文件的名称,`as_attachment` 参数指定将文件作为附件发送,而不是内联显示。 要使用 `flasgger` 自动生成的接口文档,需要在应用根目录下创建一个名为 `swagger.yaml` 的文件,并将以下内容复制到该文件中: ```yaml swagger: '2.0' info: version: '1.0' title: Sample API description: Sample API for demonstration purposes only. paths: {} ``` 运行应用后,在浏览器中访问 `http://localhost:5000/download` 就可以下载接口文档了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bruce-li__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值