Python连接不显示Swagger UI [英] Python connexion not displaying Swagger UI

查看:46
本文介绍了Python连接不显示Swagger UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用connexion模块构建了一个基于Python/Flask的REST API.与swagger.yml文件一起定义REST API的效果很好.该应用程序正在运行,但是当我导航到/ui时,我在浏览器中看到的全部是:

I've built a Python/Flask based REST API using the connexion module. This working well as defining the REST API with with swagger.yml file works very well. The application is running, but when I navigate to /ui all I get in my browser is this:

我尚未禁用UI,所以我不确定发生了什么以及为什么未显示UI.我的应用程序没有/static文件夹(它只是一个API),因此该应用程序未提供任何静态文件,不确定是否与问题相关.

I haven't disabled the UI, so I'm not sure what's going on and why the UI isn't being displayed. My application doesn't have a /static folder (it's only an API), so the app isn't serving any static files, not sure if that's related to the problem or not.

任何有关我做错事情的建议,指示或提示,将不胜感激!

Any suggestions, pointers or hints about what I'm doing wrong would be most appreciated!

这是我的代码的简化示例:

Here is a simplified example of my code:

# 3rd party libraries
from flask_cors import CORS
import connexion

def create_app(config_key, instance_config=None):
    # create the connexion instance
    connex_app = connexion.FlaskApp(__name__, specification_dir='./files/swagger/')
    connex_app.server = 'gevent'

    # get the Flask app instance
    app = connex_app.app

    # configure the application
    app.config.from_object(config_key)

    # add CORS support to application
    CORS(app)

    # define the API with the SWAGGER API definition YAML file
    connex_app.add_api('line_controller_api.yml',
                       base_path='{url_prefix}'.format(url_prefix=app.config.get('URL_PREFIX', '/')),
                       resolver=AppResolver())

    return connex_app


def production_app(instance_config=None):
    app = create_app('api_config.ProductionConfig', instance_config)
    return app

if __name__ == '__main__':
    app = create_app('api_config.DevelopmentConfig')
    port = 5001
    logger.info('Line Controller API running on port %s', port)
    app.run(host='0.0.0.0', port=port)

预先感谢,道格

推荐答案

连接中没有捆绑 swagger-ui .您已经使用以下命令显式安装了它(请注意引号)

connexion from 2.0.1 version onward don't have swagger-ui bundled inside it. You have install it explicitly using the below command (note the quotes)

pip install 'connexion[swagger-ui]'

安装后.昂首阔步将与连接.在较早的版本中,用于与/ui一起使用的swagger被添加到了 http(s)://host:port

Once you install it. swagger will work with connexion. In the earlier version swagger used to work with /ui added to your url at the end http(s)://host:port

但是从2.0.x开始,请使用 http(s)://host:port/< basepath>/ui

But in 2.0.x onward use http(s)://host:port/<basepath>/ui

这篇关于Python连接不显示Swagger UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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