Reload Flask app when template file changes
you can use
TEMPLATES_AUTO_RELOAD = True
From http://flask.pocoo.org/docs/1.0/config/
Whether to check for modifications of the template source and reload it automatically. By default the value is None which means that Flask checks original file only in debug mode.
In my experience, templates don't even need the application to restart to be refreshed, as they should be loaded from disk everytime render_template()
is called. Maybe your templates are used differently though.
To reload your application when the templates change (or any other file), you can pass the extra_files
argument to Flask().run()
, a collection of filenames to watch: any change on those files will trigger the reloader.
Example:
from os import path, walkextra_dirs = ['directory/to/watch',]extra_files = extra_dirs[:]for extra_dir in extra_dirs: for dirname, dirs, files in walk(extra_dir): for filename in files: filename = path.join(dirname, filename) if path.isfile(filename): extra_files.append(filename)app.run(extra_files=extra_files)
See here: http://werkzeug.pocoo.org/docs/0.10/serving/?highlight=run_simple#werkzeug.serving.run_simple