from flask import Flask, jsonify
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
app = Flask('Test')# 1. 通过swagger_from_file关键词搜索对应的文档@app.route('/fun_json')defspec():return jsonify(swagger(app, from_file_keyword="swagger_from_file"))# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(
base_url='/api/docs',# URL for exposing Swagger UI (without trailing '/')
api_url='http://127.0.0.1:5003/fun_json',# Our API url (can of course be a local resource)
config={# Swagger UI config overrides'app_name':"测试应用",'title':"测试应用"},)# 2. swagger_from_file指定对应的配置文件,其中yaml文件的配置@app.route('/def_fun', methods=["POST"])deflogin():"""
swagger_from_file: test.yaml
"""return'login success'
app.register_blueprint(swaggerui_blueprint)if __name__ =='__main__':
app.run(host='127.0.0.1', port='5003', debug=False, use_reloader=False)# 浏览器打开http://127.0.0.1:5003/api/docs/
yaml文件
Create a new user
---tags:- users
definitions:-schema:id: Group
properties:name:type: string
description: the group's name
parameters:-in: body
name: body
schema:id: User
required:- email
- name
properties:email:type: string
description: email for user
name:type: string
description: name for user
address:description: address for user
schema:id: Address
properties:street:type: string
state:type: string
country:type: string
postalcode:type: string
groups:type: array
description: list of groups
items:$ref:"#/definitions/Group"responses:201:description: User created