简体   繁体   English

如何将 hash 字符串添加到 Bokeh 应用程序中的自定义资源 url?

[英]How to add hash strings to custom resources urls in a Bokeh Application?

I am programming a Bokeh Server Application with templates.我正在使用模板编写散景服务器应用程序。 I have added some custom CSS and JS files.我添加了一些自定义 CSS 和 JS 文件。 But when I make some changes the files are reloaded from the cache and I cannot see the new changes.但是当我进行一些更改时,文件会从缓存中重新加载,我看不到新的更改。 One solution is to add a variable at the end of the URL in order to load them from scratch just if there were some changes.一种解决方案是在 URL 的末尾添加一个变量,以便在有一些更改时从头开始加载它们。 Bokeh resources work like that.散景资源就是这样工作的。

I have added the resources as in this example so far到目前为止,我已经添加了本示例中的资源

{% extends base %}

<!-- goes in head -->
{% block preamble %}
    <link href="app/static/css/custom.min.css" rel="stylesheet">
    <script type="text/javascript" src="app/static/js/custom.min.js"></script>
{% endblock %}

<!-- goes in body -->
{% block contents %}
    <div> {{ embed(roots.scatter) }} </div>
    <div> {{ embed(roots.line) }} </div>
{% endblock %}

Is there a builtin way to add these hashes?有没有内置的方法来添加这些哈希? The result I would like to have is:我想要的结果是:

<link href="app/static/css/custom.min.css?v=f5ee62ee57a37d94c35e8f7be218e11fc63df459" rel="stylesheet">

This hash string must be added before the page is loaded to make it work well必须在页面加载之前添加此 hash 字符串以使其正常工作

I found this class , but it seems just for Bokeh resources我找到了这个 class ,但它似乎只是为了散景资源

Bokeh uses Jinja2 templates but it doesn't provide any access to template variables. Bokeh 使用 Jinja2 模板,但它不提供对模板变量的任何访问。 One of possible workarounds is to create a custom template class:一种可能的解决方法是创建自定义模板 class:

from hashlib import md5

from jinja2 import Template

from bokeh.io import save
from bokeh.models import Div


class ExtendedTemplate(Template):
    def render(self, *args, **kwargs):
        if 'with_hash' not in kwargs:
            kwargs['with_hash'] = with_hash
        return super().render(*args, **kwargs)


t = ExtendedTemplate("""\
<link href="{{ with_hash('app/static/css/custom.min.css') }}" rel="stylesheet">
""")


def with_hash(s):
    with open(s, 'rb') as f:
        return f'{s}?v={md5(f.read()).hexdigest()}'


save(Div(), template=t)

Note that this particular code assumes that the CSS file's path is accessible from its current working directory.请注意,此特定代码假定 CSS 文件的路径可从其当前工作目录访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM