Flask-Swagger-UI无法识别swagger.json的路径 [英] Flask-Swagger-UI does not recognize path to swagger.json

查看:147
本文介绍了Flask-Swagger-UI无法识别swagger.json的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask和flask-restful和flask-swagger-ui构建API.现在,我已经修改了项目结构,现在无法再访问该项目的swagger.json文件.

根据软件包文档

我的代码:

从烧瓶导入Flask的

 ,jsonify从flask_migrate导入迁移从flask_restful导入Api从flask_swagger_ui导入get_swaggerui_blueprintdef create_app(config_name):app = Flask(__ name__)app.config.from_object(config [config_name])api = Api(app,prefix ="/api/v1")``特定于摇摆人''SWAGGER_URL ='/api/v1/docs'#API_URL ='templates/swagger.json'API_URL ='app/templates/docs/swagger.json'SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(SWAGGER_URL,API_URL,config = {'app_name':我的休息应用"})app.register_blueprint(SWAGGERUI_BLUEPRINT,url_prefix = SWAGGER_URL)db.init_app(app)迁移(应用,数据库)返回应用 

我的树结构:

 <代码>├──API│├──应用││├──__init__.py││├──型号│││├──__init__.py│││├──db.py│││└──db2.py││├──路线│││├──__init__.py│││├──resources.py││└──模板││└──docs││└──swagger.json│├──app.db│├──config.py│├──main.py│├──迁移│├──要求│└──测试└──README.md 

我需要帮助,以了解文件路径的问题,从而解决问题.

解决方案

我认为这是对烧瓶末端的限制,但看来您必须将静态文件放置在显式命名为 flask应用程序根目录中的> static/.

尝试更改

  API_URL ='app/templates/docs/swagger.json' 

  API_URL ='/static/swagger.json' 

然后在 API/app/中创建一个 static/文件夹,并将json移入其中.像这样: API/app/static/swagger.json

如果这不起作用,请确保您在正确的目录中具有 static/文件夹,然后在定义flask应用程序变量后尝试 print(app.root_path)为了查看什么烧瓶认为根路径是.

I'm building an API, using Flask and flask-restful and flask-swagger-ui. I have now modified the project structure and now I can no longer access the project's swagger.json file.

Based on the package documentation flask-swagger-ui, you would only need to change the parameter API_URL to the correct path. But even when entering relative path or full path, I can no longer access the file.

My Code:

from flask import Flask, jsonify
from flask_migrate import Migrate
from flask_restful import Api
from flask_swagger_ui import get_swaggerui_blueprint

def create_app(config_name):

    app = Flask(__name__)

    app.config.from_object(config[config_name])

    api = Api(app, prefix="/api/v1")

    '''swagger specific'''
    SWAGGER_URL = '/api/v1/docs'
    # API_URL = 'templates/swagger.json'
    API_URL = 'app/templates/docs/swagger.json'
    SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
        SWAGGER_URL,
        API_URL,
        config={
            'app_name': "My Rest App"
        }
    )

    app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)

    db.init_app(app)
    Migrate(app, db)

    return app

My Tree Structure:

├── API
│   ├── app
│   │   ├── __init__.py
│   │   ├── models
│   │   │   ├── __init__.py
│   │   │   ├── db.py  
│   │   │   └── db2.py
│   │   ├── routes
│   │   │   ├── __init__.py
│   │   │   ├── resources.py
│   │   └── templates
│   │       └── docs
│   │           └── swagger.json
│   ├── app.db
│   ├── config.py
│   ├── main.py
│   ├── migrations
│   ├── requeriments
│   └── tests
└── README.md

I need help, to understand the problem of the path to the file and thus correct the problem.

解决方案

I believe that its a restriction on flask's end, but it seems like you must place static files in a folder explicitly named static/ at the root of your flask app.

Try changing

API_URL = 'app/templates/docs/swagger.json'

to

API_URL = '/static/swagger.json'

Then create a static/ folder in API/app/, and move your json into it. Like so: API/app/static/swagger.json

If that doesn't work, make sure you have the static/ folder in the correct directory, try print(app.root_path) after defining your flask app variable in order to see what flask considered the root path to be.

这篇关于Flask-Swagger-UI无法识别swagger.json的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆