我正在尝试运行flask swagger ui,但它提示我没有找到API定义的消息。我正在使用请求获取在线swagger.json,并将其放在static > swagger.json中。无法共享联机swagger url,因为它是组织属性。
文件夹结构:
| App
| static
| swagger.json
| app.py
| requirements.txt代码:
import requests
import json
import argparse
import os
from flask import Flask, jsonify, make_response
from flask_cors import CORS
from flask_swagger_ui import get_swaggerui_blueprint
from flask import Blueprint
REQUEST_API = Blueprint('request_api', __name__)
PATH = "C:/Users/swagger/api/static/swagger.json"
def get_blueprint():
"""Return the blueprint for the main app module"""
return REQUEST_API
APP = Flask(__name__)
### swagger specific ###
SWAGGER_URL = '/swagger'
API_URL = '/static/swagger.json'
json_data = requests.get('https://url/api/swagger.json')
data = json_data.content
with open(PATH, 'wb') as f:
f.write(data)
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "API"
}
)
APP.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
### end swagger specific ###
APP.register_blueprint(get_blueprint())
if __name__ == '__main__':
PARSER = argparse.ArgumentParser(
description="API doc")
PARSER.add_argument('--debug', action='store_true',
help="Use flask debug/dev mode with file change reloading")
ARGS = PARSER.parse_args()
PORT = int(os.environ.get('PORT', 5000))
if ARGS.debug:
print("Running in debug mode")
CORS = CORS(APP)
APP.run(host='0.0.0.0', port=PORT, debug=True)
else:
APP.run(host='0.0.0.0', port=PORT, debug=False)错误:

发布于 2020-07-30 00:07:14
flask-swagger-ui的基本示例明确表示接受本地资源。因此,这应该是可行的:
from pathlib import Path
API_URL = Path("C:/Users/swagger/api/static/swagger.json") # maybe you need a .resolve() if Path objects are not supportedhttps://stackoverflow.com/questions/63154447
复制相似问题