1

I have a flask app that is accessed using the /get_data endpoint and providing a number as an id (127.0.0.1:55555/get_data=56)

@app.route('/get_data=<id>') 
def get_vod_tree(id):
    ...
    return render_template('index.html') 

It invokes a function that fetches some data under that id and creates one json file [static/data.json] with the collected data and then returns render_template('index.html').

In the index.html there is a call to ../static/data_tree.js which in turn reads the json file and outputs a visualization in the browser using d3.js.

When the app runs, I get the same output on the browser even when I use a different id and the json file changes. It only works if I reload the app and then access the url.

My question is: How can I make sure that multiple users get the different outputs according to the id or how can I reload the app when I hit the endpoint.

Afterthought: If multiple users are using the app then there is only one file created each time

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
case
  • 175
  • 2
  • 4
  • 18
  • How are you getting the data into index.html? – Sinan Guclu Jul 22 '16 at 09:52
  • I don't understand your process. Why having a single file overwritten each time instead of keeping all data.json and just passing `id` to the template ? The only reason I can see is because the data can change between successive calls, but then why not pass it directly to the template ? – polku Jul 22 '16 at 10:33
  • @polku Yes the data changes but it's also collected using a python function that formats the fetched json data. – case Jul 22 '16 at 10:47

2 Answers2

5

Thanks for the answers. It helped getting to the bottom of this. It works now like this:

@app.after_request
def apply_caching(response):
    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    return response

So by disabling the cache the application can treat each request as unique.

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
case
  • 175
  • 2
  • 4
  • 18
-1

If you set app.run(debug=True) in your test/dev environment, it will automatically reload your Flask app every time a code change happens.

To reload the application when a static file changes, use the extra_files parameter. See here.

Example code:

extra_dirs = ['directory/to/watch',]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
    for dirname, dirs, files in os.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)

Source

simanacci
  • 2,197
  • 3
  • 26
  • 35