Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canonical way to run Flask app locally

Tags:

python

flask

The official Flask documentation uses flask run or python -m flask run, both of which require that FLASK_APP be set. Most other tutorials I've seen, however, simply use python app.py, which doesn't require the extra step and which has worked well for me so far.

What are the advantages of flask run, if any? I want to make sure that the alternative doesn't lead to a bug that I can't figure out later on.

like image 329
bongbang Avatar asked Aug 22 '16 20:08

bongbang


1 Answers

Unless you have a reason not to (and you probably don't), use flask run to run the development server. It is what is supported going forward. Paraphrasing from the docs:

from Flask 0.11 onward the flask command is recommended. The reason for this is that due to how the dev server's reload mechanism works there are some bizarre side-effects when using app.run (like executing certain code twice, sometimes crashing without message or dying when a syntax or import error happens).

To solve these problems, the flask command separates the app from the code that imports the app and runs the server. The flask.run method still exists because none of those issues were critical, only confusing. It may be fully deprecated in the future.

Besides the run command, it also provides the ability to add other useful commands that can be run inside the app context, in place of separate extensions or scripts.

As always, the same warning still applies: do not run the development server in production.

like image 83
davidism Avatar answered Oct 16 '22 19:10

davidism