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/

相关推荐
用户8356290780512 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon3 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly3 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程4 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly6 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风12 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风12 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风1 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python
明月_清风1 天前
Python 消失的内存:为什么 list=[] 是新手最容易踩的“毒苹果”?
后端·python