Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto reloading python Flask app upon code changes

I'm investigating how to develop a decent web app with Python. Since I don't want some high-order structures to get in my way, my choice fell on the lightweight Flask framework. Time will tell if this was the right choice.

So, now I've set up an Apache server with mod_wsgi, and my test site is running fine. However, I'd like to speed up the development routine by making the site automatically reload upon any changes in py or template files I make. I see that any changes in site's .wsgi file causes reloading (even without WSGIScriptReloading On in the apache config file), but I still have to prod it manually (ie, insert extra linebreak, save). Is there some way how to cause reload when I edit some of the app's py files? Or, I am expected to use IDE that refreshes the .wsgi file for me?

like image 652
Passiday Avatar asked May 02 '13 18:05

Passiday


People also ask

Why does running the flask dev server run itself twice?

Answer #1: The Werkzeug reloader spawns a child process so that it can restart that process each time your code changes. Werkzeug is the library that supplies Flask with the development server when you call app.

Is flask a sync or async?

After seeing what just happened, next time people say flask is synchronous and it can handle only one request at a time. Knowing that by configuring thread, process at app. run, you know for sure we will be able to handle more than a request at a time.


2 Answers

The current recommended way is with the flask command line utility.

https://flask.palletsprojects.com/en/1.1.x/quickstart/#debug-mode

Example:

$ export FLASK_APP=main.py $ export FLASK_ENV=development $ flask run 

or in one command:

$ FLASK_APP=main.py FLASK_ENV=development flask run 

If you want different port than the default (5000) add --port option.

Example:

$ FLASK_APP=main.py FLASK_ENV=development flask run --port 8080 

More options are available with:

$ flask run --help 

FLASK_APP can also be set to module:app or module:create_app instead of module.py. See https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery for a full explanation.

like image 150
Eyal Levin Avatar answered Oct 02 '22 15:10

Eyal Levin


If you are talking about test/dev environments, then just use the debug option. It will auto-reload the flask app when a code change happens.

app.run(debug=True) 

Or, from the shell:

$ export FLASK_DEBUG=1 $ flask run 

http://flask.pocoo.org/docs/quickstart/#debug-mode

like image 31
codegeek Avatar answered Oct 02 '22 15:10

codegeek