arthuston
arthuston

Reputation: 257

How to run a Connextion/Flask app using flask?

We have an existing flask app that we'd like to move to connexion. Can the app still be run using flask, e.g. 'flask run'. We use a 'manage' plugin in the cli that we'd like to keep using.

We tried modifying the app factory method to use the connexion flask app. Using 'run flask' we get an error that 'app' is not a flask app.

    app = connexion.FlaskApp(__name__.split('.')[0])
    app.add_api('some_api.yml')
    return app

Also tried creating both apps.

    app = Flask(__name__.split('.')[0])
    connexion_app = connexion.FlaskApp(__name__.split('.')[0])
    connexion_app.add_api('./api/resources/reload_spec.yml')
    return app

Running 'flask run' with the first approach gives an error that 'app' is not a valid flask app: RuntimeError: app is not a valid flask.app.Flask app instance

The second approach does not seem to be generating the UI, which is not surprising since the connexion_app is not being run.

Is this even possible?

Upvotes: 1

Views: 3917

Answers (3)

arthuston
arthuston

Reputation: 257

    # factory.py

    # connexion app
    app = connexion.FlaskApp(__name__.split('.')[0])
    app.add_api('some_api.yml')

    # flask app
    app = app.app
    # flask-specific code
    return app

This works with flask run, and displays the swagger docs.

$ flask run
 * Serving Flask app "app/app.py"
<snip>
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)`

Upvotes: 7

Kevin Martins
Kevin Martins

Reputation: 598

Change app = connexion.FlaskApp(__name__.split('.')[0]) to app = connexion.App(__name__.split('.')[0])

Upvotes: 0

Akalanka Weerasooriya
Akalanka Weerasooriya

Reputation: 542

# app.py
app = connexion.FlaskApp(__name__.split('.')[0])
app.add_api('some_api.yml')
app.run(port=8080)

Above should run with python app.py

To generate UI, use pip install connexion[swagger-ui] and navigate to {base_path}/ui/

Based on https://github.com/zalando/connexion

Upvotes: 0

Related Questions