Python connection not displaying Swagger UI

11.2k views Asked by At

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 <my-app-url>/ui all I get in my browser is this:

enter image description here

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.

What am I doing wrong?

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

There are 4 answers

1
Galuoises On

I had the same problem. I solved it with

pip install pathlib swagger_ui_bundle
0
Slapy On

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.

0
Chris Hilton On

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. 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. This is the shell syntax you need in Unix/Linux to properly install the swagger-ui plugin:

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. Furthermore, I spent a lot of time scratching my head on this one because I did this in a virtualenv. I also searched the virtualenv for the swagger-ui component with find and I found some stub installed. So, if you are new to python or you are in a hurry, this can be easy to miss.

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.

1
Ashraff Ali Wahab On

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]'

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

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