Reload Flask app when template file changes
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, walk
extra_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
Auto reloading python Flask app upon code changes
Run the flask run
CLI command with debug mode enabled, which will automatically enable the reloader. As of Flask 2.2, you can pass --app
and --debug
options on the command line.
$ flask --app main.py --debug run
--app
can also be set to module:app
or module:create_app
instead of module.py
. See the docs for a full explanation.
More options are available with:
$ flask run --help
Prior to Flask 2.2, you needed to set the FLASK_APP
and FLASK_ENV=development
environment variables.
$ export FLASK_APP=main.py
$ export FLASK_ENV=development
$ flask run
It is still possible to set FLASK_APP
and FLASK_DEBUG=1
in Flask 2.2.
Reload Flask app when template file changes under Linux
I managed to fix my issue by adding my template folder to extra_files parameter of Flask app
Here is how :
extra_dirs = [
'/home/karim/flak_app/templates',
]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
for dirname, dirs, files in os.walk(extra_dir):
for filename in files:
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
extra_files.append(filename)
app.run(threaded=True, extra_files=extra_files)
Hope this will help someone someday :)
Is there a way to reload a flask app without code or file changes?
If you are running your application as a service (for example via systemctl
), just call one of the many functions available in python to execute a command.
For example:
import os
@app.route('/reloadFlask', methods = ['POST'])
def reloadFlask():
os.system("systemctl restart <your_service_name>")
or:
import subprocess
@app.route('/reloadFlask', methods = ['POST'])
def reloadFlask():
p = subprocess.Popen(["systemctl", "restart", "<your_service_name>"], stdout=subprocess.PIPE)
out, err = p.communicate()
How can i update flask templates without restarting the uwsgi instance?
Enabling TEMPLATES_AUTO_RELOAD
works nicely:
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
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.
Source: http://flask.pocoo.org/docs/0.12/config/
Related Topics
How to Rotate a Matplotlib Plot Through 90 Degrees
Python Re.Sub Back Reference Not Back Referencing
Weird Behavior: Lambda Inside List Comprehension
Repeat Rows in Data Frame N Times
How to Melt 2 Columns at the Same Time
Sparse Matrix Slicing Using List of Int
Restricting the Value in Tkinter Entry Widget
Customising Code of Qt Designer Widget
The Main() Function Appears to Not Work
Cannot List Ftp Directory Using Ftplib - But Ftp Client Works
Can Multiprocessing Process Class Be Run from Idle
How to Access a Function Inside a Function
Unicodeencodeerror: 'Ascii' Codec Can't Encode Character '\Xe9' - -When Using Urlib.Request Python3
Using a Where _ in _ Statement
Qwidget Does Not Draw Background Color
Validation of a Password - Python
Python - Pysftp/Paramiko - Verify Host Key Using Its Fingerprint