2

I've got a plotly visualization ('viz.html') in a html format. I've embedded it in my webpage using Flask's url_for() syntax, referencing the static folder:

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

It deploys with no issues. However, my 'viz.html' will be rewritten frequently, updating itself with new data points. The updated visualization won't show with a control + R or webpage refresh. If I press control + F5 and do a cache refresh, then updated visualization will show.

I want users to be able to see the updated visualization without having to manually refresh the cache. So far I've tried:

Reloading flask app when file changes are detected in 'static' folder (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)

Disabling browser cacheing on html page (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" />

Disabling Python on Flask Cacheing

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

Would like to know, is what I'm trying to do even possible?

If it's possible how can I go about doing it? Advice appreciated!

davidism
  • 121,510
  • 29
  • 395
  • 339
Benjamin
  • 21
  • 1

2 Answers2

0

I am not a web developer but I worked with a few closely for 3 years. As far as I know, there is no way to control what other browsers saved as cache. If my browser has a version saved in the cache, it will use that one. Usually, browsers will check if all the files are updated, and eventually, it will refresh for everyone. I actually heard developers being asked "why didn't you fix what I told you?" and they were like, "I have, just hard refresh." So I don't think there is a way. IDK if anything new is out there.

circuito
  • 33
  • 4
  • Yes I think you're right, instead of writing it to a cached .html file, I have the visualization rendered in my app itself. Problem solved! – Benjamin Jan 22 '21 at 07:25
0

I figured it out!

Instead of writing the plotly fig to a viz.html file, do viz = Markup(fig) in flask:

@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)

In foo.html, you can directly use viz as so:

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

viola!

Benjamin
  • 21
  • 1