在模板文件更改时重新装载Flask应用程序 [英] Reload Flask app when template file changes

查看:1099
本文介绍了在模板文件更改时重新装载Flask应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,当使用内置服务器( Flask.run )运行Flask应用程序时,它会监视其Python文件,并在代码更改时自动重新载入应用程序:

  *在'/home/xion/hello-world/app.py'中检测到更改,重新加载$ ​​b $ b *重新启动with reloader 

不幸的是,这似乎只适用于 *。py 文件,而且我似乎没有找到任何方法将此功能扩展到其他文件。最值得注意的是,当模板更改时,Flask重新启动应用程序将非常有用。我已经失去了多少次在模板中加入标记,并且由于没有看到任何更改而感到困惑,只是发现应用程序仍然使用旧版本的Jinja模板。



那么,在 templates 目录中是否有Flask监视器文件的一种方法,或者是否需要跳入框架的源代码?



编辑:我正在使用Ubuntu 10.10。真的没有尝试过任何其他平台。




经过进一步的调查,我发现模板的变化确实 实时更新,无需重新加载应用程序本身。但是,这似乎只适用于那些传递给 flask.render_template 的模板。



在我的应用程序中,我有很多可重用的参数化组件,我在Jinja模板中使用。它们被实现为 {%macro%} ,位于专用的模块中,并且是 {%import%} 编辑成实际页面。所有漂亮和干燥的...除了那些导入的模板显然从来没有检查修改,因为他们不通过 render_template 根本。



(奇怪的是,通过 {%extends%} 调用的模板不会发生这种情况。至于 {%include %} ,我不知道,因为我没有真正使用它们。)

所以,总结一下,这个现象的根源似乎躺在Jinja和Flask或Werkzeug之间。我想这可能需要为这两个项目中的任何一个错误跟踪器:)此外,我接受了 jd。的答案,因为这是我实际使用的解决方案 - 它的作用就像一个魅力。

解决方案

根据我的经验,模板甚至不需要重新启动应用程序来刷新,因为它们应该从磁盘每次调用 render_template()。也许你的模板的用法不同。

要在模板更改(或其他文件)时重新加载应用程序,可以将 extra_files 参数转换为 Flask()。run(),这是要监视的文件名的集合:对这些文件的任何改变都会触发重载。


$ b

示例:

  from os导入路径

extra_dirs = ['directory / to / watch',]
extra_files = extra_dirs [:] $ b $ for extra_dir中的extra_dirs:
for dirname,dirs,os.walk中的文件(extra_dir):
为文件名:
filename = path.join(dirname,filename)
如果path.isfile(文件名):
extra_files.append(文件名)
app.run (extra_files = extra_files)

请参阅 http://werkzeug.pocoo.org/docs/0.10/serving/?hig hlight = run_simple#werkzeug.serving.run_simple


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.

解决方案

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

extra_dirs = ['directory/to/watch',]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
    for dirname, dirs, files in os.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

这篇关于在模板文件更改时重新装载Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆