【问题标题】:Dynamic plotly visualization (or image) in Flask PythonFlask Python 中的动态绘图可视化(或图像)
【发布时间】:2021-04-08 04:45:26
【问题描述】:

我有一个 html 格式的绘图可视化('viz.html')。我使用 Flask 的 url_for() 语法将它嵌入到我的网页中,并引用了静态文件夹:

<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="{{ url_for('static', filename='viz.html') }}" height="400" width="120%"></iframe>

它部署没有问题。但是,我的“viz.html”会经常被重写,用新的数据点更新自己。 control + R 或网页刷新不会显示更新的可视化效果。如果我按control + F5 并刷新缓存,则会显示更新的可视化。

我希望用户能够看到更新后的可视化,而无需手动刷新缓存。到目前为止,我已经尝试过:

在“静态”文件夹中检测到文件更改时重新加载烧瓶应用 (Reload Flask app when template file changes)

check_folder = './static'
check_files = []
for dirname, dirs, files in os.walk(check_folder):
    for filename in files:
        filename = os.path.join(check_folder, filename)
        check_files += [filename]
app.run(debug = True, extra_files = check_files)

在 html 页面上禁用浏览器缓存 (http://cristian.sulea.net/blog/disable-browser-caching-with-meta-html-tags)

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

在 Flask 缓存中禁用 Python

resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
resp.headers["Pragma"] = "no-cache"
resp.headers["Expires"] = "0"

想知道,我正在尝试做的事情是否可能?

如果可能的话,我该怎么做?建议赞赏!

【问题讨论】:

    标签: python flask plotly


    【解决方案1】:

    我想通了!

    不要将 fig 写入 viz.html 文件,而是在烧瓶中执行 viz = Markup(fig)

    @app.route("/index/",methods=["POST", "GET"])
        def foo():
            from flask import Markup
            # include_plotlyjs = True builds in the js library
            # output_type = 'div' outputs the html code
            viz = plot(fig, include_plotlyjs = True, output_type = 'div')
        
            # Markup directly renders the html code
            viz = Markup(viz)
            return render_template('foo.html', viz = viz)
    

    foo.html中,可以直接使用viz,如下:

    <html>
        <body>
            viz
        </body>
    <html>
    

    中提琴!

    【讨论】:

      【解决方案2】:

      我不是网络开发人员,但我与一些人密切合作了 3 年。据我所知,没有办法控制其他浏览器保存为缓存。如果我的浏览器在缓存中保存了一个版本,它将使用那个版本。通常,浏览器会检查是否所有文件都更新了,最终它会为每个人刷新。我实际上听到开发人员被问到“你为什么不修复我告诉你的内容?”他们就像,“我有,只是很难刷新。”所以我认为没有办法。如果有任何新内容,请 IDK。

      【讨论】:

      • 是的,我认为您是对的,我没有将其写入缓存的 .html 文件,而是在我的应用程序本身中呈现了可视化。问题解决了!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多