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/

相关推荐
cup112 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi004 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵5 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf6 小时前
Agent 流程编排
后端·python·agent
copyer_xyf7 小时前
Agent RAG
后端·python·agent
copyer_xyf7 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf7 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏