Python Flask flasgger api文档[python/flask/flasgger]

首先需要安装依赖:

python 复制代码
pip install flasgger

封装swagger.py文件,代码如下:

python 复制代码
from flasgger import Swagger
swagger = Swagger()

然后在主应用中(项目入口文件)加入以下代码:

python 复制代码
from flask import Flask 
from swagger import Swagger #导入封装好的swagger.py文件中的Swagger对象
# 创建 Flask 实例
app = Flask(__name__)
Swagger(app) 

然后在user.py文件中加入以下代码:

python 复制代码
#举例
@user_bp.route('/info', methods=['GET'])
def user_info():
    """获取用户信息
        ---
        parameters:
          - name: Authorization
            in: header
            required: true
            description: 用户token
            type: string
        responses:
          200:
            description: 成功
            schema:
              properties:
                code:
                  type: integer
                msg:
                  type: string
                data:
                  type: object
          401:
            description: 失败
        """
    userInfo = get_jwt_identity()
    if not userInfo:
        return r(msg='暂未登录')
    else:
        return r(msg='',data=userInfo)

运行项目,

访问:http://127.0.0.1:5000/apidocs/

相关推荐
程序员的奶茶馆4 分钟前
Python 数据结构面试真题:如何实现 LRU 缓存机制
python·面试
星期天要睡觉20 分钟前
深度学习——基于 ResNet18 的图像分类训练
pytorch·python·机器学习
林炳然21 分钟前
Python-Basic Day-1 基本元素(数字、字符串)
python
weixin_3077791323 分钟前
在Linux服务器上使用Jenkins和Poetry实现Python项目自动化
linux·开发语言·python·自动化·jenkins
润 下24 分钟前
C语言——深入解析C语言指针:从基础到实践从入门到精通(四)
c语言·开发语言·人工智能·经验分享·笔记·程序人生·其他
今天没有盐25 分钟前
内置基础类型之布尔值类型(bool)与时间与日期类型
python·编程语言
Empty_77728 分钟前
Python编程之常用模块
开发语言·网络·python
小火柴12331 分钟前
利用R绘制箱线图
开发语言·r语言
wheeldown42 分钟前
【Linux】Linux 进程通信:System V 共享内存(最快方案)C++ 封装实战 + 通信案例,4 类经典 Bug 快速修复
linux·运维·服务器·开发语言
小年糕是糕手1 小时前
【数据结构】双向链表“0”基础知识讲解 + 实战演练
c语言·开发语言·数据结构·c++·学习·算法·链表