Reload Flask app when template file changes

前端 未结 10 1514
栀梦
栀梦 2020-11-27 13:04

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 cha

相关标签:
10条回答
  • 2020-11-27 13:18

    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

    0 讨论(0)
  • 2020-11-27 13:21

    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.

    0 讨论(0)
  • 2020-11-27 13:22

    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:

    if __name__ == '__main__':
        app.jinja_env.auto_reload = True
        app.config['TEMPLATES_AUTO_RELOAD'] = True
        app.run(debug=True, host='0.0.0.0')
    
    0 讨论(0)
  • 2020-11-27 13:28

    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.

    0 讨论(0)
  • 2020-11-27 13:31

    Templates are reloaded automatically, why not doing ctrl+f5 to refresh the webpage, cause web-browsers usually save cache.

    0 讨论(0)
  • 2020-11-27 13:32

    Updated as of June 2019:

    The flask CLI is recommended over app.run() for running a dev server, so if we want to use the CLI then the accepted solution can't be used.

    Using the development version of Flask (1.1) as of this writing allows us to set an environment variable FLASK_RUN_EXTRA_FILES which effectively does the same thing as the accepted answer.

    See this github issue.

    Example usage:

    export FLASK_RUN_EXTRA_FILES="app/templates/index.html"
    flask run
    

    in Linux. To specify multiple extra files, separate file paths with colons., e.g.

    export FLASK_RUN_EXTRA_FILES="app/templates/index.html:app/templates/other.html"
    

    The CLI also supports an --extra-files argument as of Flask 1.1.

    0 讨论(0)
提交回复
热议问题