[英]Python connexion not displaying Swagger UI
I've built a Python/Flask based REST API using the connexion module.我已经使用 connexion 模块构建了一个基于 Python/Flask 的 REST API。 This working well as defining the REST API with with swagger.yml file works very well.这与使用 swagger.yml 文件定义 REST API 非常有效。 The application is running, but when I navigate to /ui all I get in my browser is this:应用程序正在运行,但是当我导航到 /ui 时,我在浏览器中得到的只是:
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.我的应用程序没有 /static 文件夹(它只是一个 API),因此该应用程序不提供任何静态文件,不确定这是否与问题有关。
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)
Thanks in advance, Doug提前致谢,道格
connexion from 2.0.1 version onward don't have swagger-ui bundled inside it.从 2.0.1 版本开始的connexion内部没有捆绑swagger-ui 。 You have install it explicitly using the below command (note the quotes)您已使用以下命令明确安装它(注意引号)
pip install 'connexion[swagger-ui]'
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
在早期版本中,swagger 用于将 /ui 添加到您的 url 末尾http(s)://host:port
But in 2.0.x onward use http(s)://host:port/<basepath>/ui
但是在 2.0.x 以后使用http(s)://host:port/<basepath>/ui
My stackoverflow reputation is too low for me to comment on Ashraff Ali Wahab 's answer above but I just found out that I can edit it myself.我的 stackoverflow 声誉太低,无法评论Ashraff Ali Wahab上面的回答,但我才发现我可以自己编辑它。 Suffice it to say that it fixed the problem for me after I understood that the shell syntax, as presented, is wrong which was pointed out by Pablo Marin-Garcia .我只想说,在我理解了所呈现的 shell 语法是错误的之后,它为我解决了问题, Pablo Marin-Garcia指出了这一点。 This is the shell syntax you need in Unix/Linux to properly install the swagger-ui plugin:这是在 Unix/Linux 中正确安装 swagger-ui 插件所需的 shell 语法:
pip install 'connexion[swagger-ui]'
Any matched quotes will do.任何匹配的引号都可以。 Note well that without the quotes, the pip command will run successfully but it won't install the swagger-ui component as you expect it to.请注意,如果没有引号,pip 命令将成功运行,但不会像您期望的那样安装 swagger-ui 组件。 Furthermore, I spent a lot of time scratching my head on this one because I did this in a virtualenv.此外,我花了很多时间在这个问题上摸不着头脑,因为我是在 virtualenv 中做到的。 I also searched the virtualenv for the swagger-ui component with find
and I found some stub installed.我还使用find
在 virtualenv 中搜索了 swagger-ui 组件,我发现安装了一些存根。 So, if you are new to python or you are in a hurry, this can be easy to miss.因此,如果您不熟悉 Python 或赶时间,这很容易错过。
At the end of the day, I decided to add a local_requirement.txt file listing the correct version of Werzueg, connexion, and "connexion[swagger-ui]" which I install before using the stock requirements.txt because it seems like the Flask API code generated by the SmartBear tools is a little dated.在一天结束时,我决定添加一个local_requirement.txt文件,其中列出了正确版本的 Werzueg、connexion 和“connexion[swagger-ui]”,我在使用 stock requirements.txt之前安装了它,因为它看起来像 Flask SmartBear 工具生成的 API 代码有点过时了。
I had the same problem.我有同样的问题。 I solved it with我解决了
pip install pathlib swagger_ui_bundle
It's caused by missing trailing slash.这是由于缺少尾部斜杠引起的。 Just add slash at the end of your url and it will work.只需在您的网址末尾添加斜杠即可。
Related issue https://github.com/zalando/connexion/issues/346 .相关问题https://github.com/zalando/connexion/issues/346 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.