首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flask Swagger UI无法找到swagger.json

Flask Swagger UI无法找到swagger.json
EN

Stack Overflow用户
提问于 2020-07-29 21:13:23
回答 1查看 838关注 0票数 1

我正在尝试运行flask swagger ui,但它提示我没有找到API定义的消息。我正在使用请求获取在线swagger.json,并将其放在static > swagger.json中。无法共享联机swagger url,因为它是组织属性。

文件夹结构:

代码语言:javascript
复制
| App
    | static
           | swagger.json
    | app.py
    | requirements.txt

代码:

代码语言:javascript
复制
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)

错误:

EN

回答 1

Stack Overflow用户

发布于 2020-07-30 00:07:14

flask-swagger-ui的基本示例明确表示接受本地资源。因此,这应该是可行的:

代码语言:javascript
复制
from pathlib import Path

API_URL = Path("C:/Users/swagger/api/static/swagger.json")  # maybe you need a .resolve() if Path objects are not supported
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63154447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档