flask: Jinja templates do not auto reload if Flask app debug enabled solely via app.run(debug=True)
When enabling debug mode via app.run(debug=True), code changes result in an auto-reload but Jinja templates are cached until the app is manually is restarted.
At https://github.com/pallets/flask/blob/21d595bee73e65a28201709bd1a8b97c42aff5d2/flask/app.py#L695 Flask checks if the config["TEMPLATE_AUTO_RELOAD"] is explicitly set. If not, Jinja’s options["auto_reload"] is set to app.debug.
However, if you instantiate your Flask object then call app.run(debug=True) without loading a config, template auto reloading is not enabled (app.debug was False when the Flask app was instantiated).
At https://github.com/pallets/flask/blob/21d595bee73e65a28201709bd1a8b97c42aff5d2/flask/app.py#L839 we enable reloading code and enable the debugger. My proposed fix would be to also enable template reloading (jinja_env.auto_reload = self.debug) here.
This would result is less confusion (“Debug is enabled but my templates don’t auto reload”) and make the template reloading behaviour consistent with the code reloading behaviour (“If no config is set or debug is not explicitly set in config, and the site calls app.run(debug=True), then enable the relevant auto reloading features”).
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 19
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Moved Jinja settings deeper into webhooks.apply_webhooks_blueprint() See https://github.com/pallets/flask/issues/1907#issuecomment-225743376 — committed to openaddresses/machine by migurski 8 years ago
- Fixing theme switching Something in Flask 0.12 changed the behavior of Jinja templates. There are a couple open issues about it https://github.com/pallets/flask/issues/1907 — committed to CTFd/CTFd by ColdHeat 7 years ago
- Fixing theme switching (#277) Something in Flask 0.12 changed the behavior of Jinja templates. There are a couple open issues about it https://github.com/pallets/flask/issues/1907 — committed to CTFd/CTFd by ColdHeat 7 years ago
- Tweak jinja cache https://github.com/pallets/flask/issues/1907 — committed to ClarkAlmazan/airborne by ClarkAlmazan 7 years ago
For me it worked with the following code:
Is this still an issue? auto reload doesn’t seem to work at all @patricksurry doing app.config explicitly doesn’t do anything for me… was wondering why this would be
Even a simple hello world app doesn’t want to reload
I think I’m running into the same issue, if I configure custom jinja filters via
@app.template_filter('format_date'). This stops auto-template reloading.If I explicitly say
app.config['TEMPLATES_AUTO_RELOAD'] = Truebefore that, reloading works again.Just to add here, since this thread helped me, the
app.jinja_env.auto_reload = Truemade it work for me again.To summarize for posterity: if you touch the Jinja2 environment in any way before the
app.runcall (e.g. adding a filter), you will need the line above beforeapp.runfor your templates to be auto-reloaded after changes. This is at least what I’ve observed.Thanks to folks here on the resolution, saved me some trouble. 😺
To those out there who may be facing the same problem using a factory pattern.
Maybe the following post has something to do with you.
https://blog.socratic.org/the-one-weird-trick-that-cut-our-flask-page-load-time-by-70-87145335f679#.biy44wahq
When I removed the following line of code from my factory pattern, I was able to just refresh the browser and the changes from the html appeared. It seems the template changes did take affect after you reload. However the flask app in terminal did not say anything about detection on the html file. Maybe we can add some sort of detection on there to let developers know what file was changed.
Hope this helps out some people.
Cheers!
Your proposed fix makes sense to me.