How to run Flask app with custom parameters?

Viewed 6711

I'm trying to run my Flask app with some additional parameters. I use click and faced the following problem. If I don't use any parameters and use just a regular flask run command the app is running but ignores my additional parameter.

python -m flask run

If I run the custom command the app executes the command code but doesn't run the app itself:

python -m flask dbinit -u

Instead it just goes through and shows the following message: " D:..\main.py:18: Warning: Silently ignoring app.run() because the application is run from the flask command line executable. Consider putting app.run() behind an if ____name____ == "____main____" guard to silence this warning."

The code:

import click
from flask.cli import with_appcontext
from app import create_app, init_database

app = create_app()


@app.cli.command()
@with_appcontext
@click.option('-u', 'dbopt', flag_value='upgrade',
              default='')
@click.option('-c', 'dbopt', flag_value='create',
              default='')
def dbinit(dbopt):
    print('init db', dbopt)
    init_database(dbopt)
    app.run(debug=False) # not working

I have env var FLASK_APP='myapp'.

The code is running on Win7. I grabbed the example from here http://flask.pocoo.org/docs/1.0/cli/#custom-commands

What am I doing wrong? Thanks.

1 Answers

You can try the run_simple from werkzeug, which is what flask cli used from what I remember. It goes something like this:

from werkzeug.serving import run_simple
return run_simple(
    hostname=host,
    port=port,
    application=app,
    use_reloader=reload,
    use_debugger=debug,
)
Related