By default, when running Flask application using the built-in server (Flask.run
), it monitors its Python files and automatically reloads the app if its code changes:
* Detected change in '/home/xion/hello-world/app.py', reloading
* Restarting with reloader
Unfortunately, this seems to work for *.py files only, and I don't seem to find any way to extend this functionality to other files. Most notably, it would be extremely useful to have Flask restart the app when a template changes. I've lost count on how many times I was fiddling with markup in templates and getting confused by not seeing any changes, only to find out that the app was still using the old version of Jinja template.
So, is there a way to have Flask monitor files in templates directory, or does it require diving into the framework's source?
Edit: I'm using Ubuntu 10.10. Haven't tried that on any other platforms really.
After further inquiry, I have discovered that changes in templates indeed are updated in real time, without reloading the app itself. However, this seems to apply only to those templates that are passed to flask.render_template
.
But it so happens that in my app, I have quite a lot of reusable, parametrized components which I use in Jinja templates. They are implemented as {% macro %}
s, reside in dedicated "modules" and are {% import %}
ed into actual pages. All nice and DRY... except that those imported templates are apparently never checked for modifications, as they don't pass through render_template
at all.
(Curiously, this doesn't happen for templates invoked through {% extends %}
. As for {% include %}
, I have no idea as I don't really use them.)
So to wrap up, the roots of this phenomenon seems to lie somewhere between Jinja and Flask or Werkzeug. I guess it may warrant a trip to bug tracker for either of those projects :) Meanwhile, I've accepted the jd.'s answer because that's the solution I actually used - and it works like a charm.
What worked for me is just adding this:
(taken from @dikkini's answer)
See http://flask.pocoo.org/docs/1.0/quickstart/ and use
FLASK_ENV=development
you can use
From http://flask.pocoo.org/docs/1.0/config/
Using the latest version of Flask on Windows, using the run command and debug set to true; Flask doesn't need to be reset for changes to templates to be brought in to effect. Try Shift+F5 (or Shift plus the reload button) to make sure nothing it being cached.
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 toFlask().run()
, a collection of filenames to watch: any change on those files will trigger the reloader.Example:
See here: http://werkzeug.pocoo.org/docs/0.10/serving/?highlight=run_simple#werkzeug.serving.run_simple
When you are working with
jinja
templates, you need to set some parameters. In my case with python3, I solved it with the following code: