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/

相关推荐
程序媛一枚~1 天前
✨✨✨使用Python,OpenCV及图片拼接生成❤️LOVE❤️字样图,每张小图加随机颜色边框,大图加随机大小随机颜色边框
图像处理·python·opencv·numpy·图像拼接
MediaTea1 天前
Python:collections.Counter 常用函数及应用
开发语言·python
如若1231 天前
flash-attn 安装失败?从报错到成功的完整排雷指南(CUDA 12.8 + PyTorch 2.7)
人工智能·pytorch·python
007张三丰1 天前
知乎高赞回答爬虫:从零开始,建立你的专属知识库
爬虫·python·知识库·python爬虫·知乎·高赞回答
LawrenceLan1 天前
37.Flutter 零基础入门(三十七):SnackBar 与提示信息 —— 页面反馈与用户交互必学
开发语言·前端·flutter·dart
李昊哲小课1 天前
Python json模块完整教程
开发语言·python·json
易醒是好梦1 天前
Python flask demo
开发语言·python·flask
怪侠_岭南一只猿1 天前
爬虫工程师入门阶段一:基础知识点完全学习文档
css·爬虫·python·学习·html