The best answers to the question “Auto reloading python Flask app upon code changes” in the category Dev.
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?
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.
Or, from the shell:
$ export FLASK_DEBUG=1 $ flask run
The current recommended way is with the
flask command line utility.
$ 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 (
$ 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:create_app instead of
module.py. See https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery for a full explanation.
If you’re running using uwsgi look at the python auto reload option:
uwsgi --py-autoreload 1
[uwsgi] socket = 127.0.0.1:5000 master = true virtualenv = /Users/xxxx/.virtualenvs/sites_env chdir = /Users/xxx/site_root module = site_module:register_debug_server() callable = app uid = myuser chmod-socket = 660 log-date = true workers = 1 py-autoreload = 1
def register_debug_server(): from werkzeug.debug import DebuggedApplication app = Flask(__name__) app.debug = True app = DebuggedApplication(app, evalex=True) return app
uwsgi --ini uwsgi-dev-example.ini
Note: This example also enables the debugger.
I went this route to mimic production as close as possible with my nginx setup. Simply running the flask app with it’s built in web server behind nginx it would result in a bad gateway error.
In test/development environments
The werkzeug debugger already has an ‘auto reload’ function available that can be enabled by doing one of the following:
app.debug = True
You can also use a separate configuration file to manage all your setup if you need be. For example I use ‘settings.py’ with a ‘DEBUG = True’ option. Importing this file is easy too;
However this is not suitable for a production environment.
Personally I chose Nginx + uWSGI over Apache + mod_wsgi for a few performance reasons but also the configuration options. The touch-reload option allows you to specify a file/folder that will cause the uWSGI application to reload your newly deployed flask app.
For example, your update script pulls your newest changes down and touches ‘reload_me.txt’ file. Your uWSGI ini script (which is kept up by Supervisord – obviously) has this line in it somewhere:
touch-reload = '/opt/virtual_environments/application/reload_me.txt'
I hope this helps!