Flask: templates_auto_reload not taken into account

Created on 3 May 2018  路  6Comments  路  Source: pallets/flask

Synopsis

If the configuration templates_auto_reload is set up after has set up it's jinja environment (example: after setting up some filters), it is ignored by Flask

Expected Behavior

Templates should be reloaded when app.config['TEMPLATES_AUTO_RELOAD'] is set to True

# application.py
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def hello(name=None):
    return render_template('hello.html', name=name)

# Templates are reloaded if configuration is set up before the template filter
# app.config.update({'TEMPLATES_AUTO_RELOAD': True})

@app.template_filter('dummy')
def dummy(value):
    return value

# Setting this up here does not do anything
app.config.update({'TEMPLATES_AUTO_RELOAD': True})

# Neither does this:
app.templates_auto_reload = True

app.run()

#% python application.py

Actual Behavior

The option is ignored because the jinja environment was already created

Environment

  • Python version: 3.6.5
  • Flask version: 1.0.2
  • Werkzeug version: 0.14.1
  • debug is set to False in this case

Most helpful comment

Is this really a problem?
Shouldn't config be set before any routes, template filters etc are defined?

All 6 comments

Is this really a problem?
Shouldn't config be set before any routes, template filters etc are defined?

Close it for now. It seems not a problem, I can't figure out a use case.

I've had this exact use case while developing my software (debug was OFF):

  • load local libraries that have template filters
  • use a configure method that updates the configuration dict
  • myapp.run() to start the service

Took me the best part of an hour to figure out the problem

@romuald why not set TEMPLATES_AUTO_RELOAD=true before .run()?

Configuration should happen as early as possible. Nothing is guaranteed to read configuration after it initializes.

@ozzywalsh that's what I've done, moved the templates filter in a specific module specifically loaded after the configuration

Was this page helpful?
0 / 5 - 0 ratings