1

I have webpage which is rendered using Flask python. In that page I am using a json file whose data will change according to the search criteria. I put the json file in a static folder. Issue is, the json data change is not getting reflected in the browser.

d3.json("{{ url_for( 'static', filename='data/Emails_export_sql.json') }}", function(graph) {
force
  .nodes(graph.nodes)
  .links(graph.links)   
  .start();
}

I tried saving the json file in folders other than 'static' folder. But it will not work. Where do I need to save the json file?

Ajoe
  • 1,397
  • 4
  • 19
  • 48

1 Answers1

1

This is a browser cache issue.

There is some discussion of it here: this SO post. Essentially the issue here is that you're requesting the same URL over and over again so the default behavior is to cache the response.

Recommendation

Use Flask's send_from_directory() function instead of the static file fetch... e.g.:

from Flask import send_from_directory

@app.route('/get_file/')
def get_file():
    # cache_timeout is in seconds from the send_file options
    return send_from_directory("data", "xyz.json", cache_timeout=5)

This will set the response headers to indicate to the browser that cache should expire in 5 seconds.

Another way to short circuit the browser cache for this resource (assuming you always want a new response) is to append a timestamp to the end of the URL:

// note... this timestamp approach fails on <IE9
var timestamp = Date.now(),
    base_url = "{{ url_for("get_file") }}",
    add_timestamp = base_url + "?t=" + timestamp;

d3.json(add_timestamp, function(graph) {
    force
      .nodes(graph.nodes)
      .links(graph.links)   
      .start();
}

In your serverside script you can just ignore the timestamp argument, but it should reliably short circuit the browser side of any cache issues.

abigperson
  • 5,252
  • 3
  • 22
  • 25