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)
提前致谢,道格
解决方案
connexion 从 2.0.1 版本开始,里面没有捆绑 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]'
安装后.swagger 将与 connexion 一起使用.在早期版本中,swagger 曾经与/ui 一起工作,在末尾添加到您的 url 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
相关文章