繁体   English   中英

Python 连接不显示 Swagger UI

[英]Python connexion not displaying Swagger UI

我已经使用 connexion 模块构建了一个基于 Python/Flask 的 REST API。 这与使用 swagger.yml 文件定义 REST API 非常有效。 应用程序正在运行,但是当我导航到 /ui 时,我在浏览器中得到的只是:

在此处输入图片说明

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

任何关于我做错了什么的建议、指示或提示将不胜感激!

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

# 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)

提前致谢,道格

从 2.0.1 版本开始的connexion内部没有捆绑swagger-ui 您已使用以下命令明确安装它(注意引号)

pip install 'connexion[swagger-ui]'

一旦你安装它。 招摇将与连接一起工作。 在早期版本中,swagger 用于将 /ui 添加到您的 url 末尾http(s)://host:port

但是在 2.0.x 以后使用http(s)://host:port/<basepath>/ui

我的 stackoverflow 声誉太低,无法评论Ashraff Ali Wahab上面的回答,但我才发现我可以自己编辑它。 我只想说,在我理解了所呈现的 shell 语法是错误的之后,它为我解决了问题, Pablo Marin-Garcia指出了这一点。 这是在 Unix/Linux 中正确安装 swagger-ui 插件所需的 shell 语法:

pip install 'connexion[swagger-ui]'

任何匹配的引号都可以。 请注意,如果没有引号,pip 命令将成功运行,但不会像您期望的那样安装 swagger-ui 组件 此外,我花了很多时间在这个问题上摸不着头脑,因为我是在 virtualenv 中做到的。 我还使用find在 virtualenv 中搜索了 swagger-ui 组件,我发现安装了一些存根。 因此,如果您不熟悉 Python 或赶时间,这很容易错过。

在一天结束时,我决定添加一个local_requirement.txt文件,其中列出了正确版本的 Werzueg、connexion 和“connexion[swagger-ui]”,我在使用 stock requirements.txt之前安装了它,因为它看起来像 Flask SmartBear 工具生成的 API 代码有点过时了。

我有同样的问题。 我解决了

pip install pathlib swagger_ui_bundle

这是由于缺少尾部斜杠引起的。 只需在您的网址末尾添加斜杠即可。

相关问题https://github.com/zalando/connexion/issues/346

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM