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

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

问题描述

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

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

不幸的是,这似乎只适用于 *.py 文件,我似乎没有找到任何方法将此功能扩展到其他文件.最值得注意的是,当模板发生变化时,让 Flask 重新启动应用程序将非常有用.我已经数不清有多少次我在模板中摆弄标记并因没有看到任何更改而感到困惑,结果却发现该应用仍在使用旧版本的 Jinja 模板.

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.

那么,有没有办法将 Flask 监控文件放在 templates 目录中,或者是否需要深入了解框架的源代码?

So, is there a way to have Flask monitor files in templates directory, or does it require diving into the framework's source?

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

Edit: I'm using Ubuntu 10.10. Haven't tried that on any other platforms really.

进一步查询后,我发现模板中的更改确实实时更新,无需重新加载应用程序本身.但是,这似乎仅适用于传递给 flask.render_template 的模板.

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.

但碰巧在我的应用程序中,我在 Jinja 模板中使用了很多可重用的参数化组件.它们被实现为 {% macro %}s,驻留在专用的模块"中,并被{% import %}ed 到实际页面中.一切都很好而且很干……除了那些导入的模板显然从未检查过修改,因为它们根本不通过 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.

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

(Curiously, this doesn't happen for templates invoked through {% extends %}. As for {% include %}, I have no idea as I don't really use them.)

总而言之,这种现象的根源似乎介于 Jinja 和 Flask 或 Werkzeug 之间.我想这可能需要为这些项目中的任何一个进行错误跟踪器之旅:) 同时,我已经接受了 jd. 的答案,因为这是我实际使用的解决方案 - 它就像一个魅力.

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.

推荐答案

根据我的经验,模板甚至不需要重新启动应用程序来刷新,因为它们应该每次都从磁盘加载render_template() 被调用.也许您的模板使用方式不同.

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.

要在模板更改(或任何其他文件)时重新加载您的应用程序,您可以将 extra_files 参数传递给 Flask().run(),这是一组要观察的文件名:对这些文件的任何更改都会触发重新加载器.

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.

示例:

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)

请参阅此处:http://werkzeug.pocoo.org/docs/0.10/serving/?highlight=run_simple#werkzeug.serving.run_simple

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

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