mardi 18 mai 2021

Flask update image without page refresh

I am trying to display a picture that changes whenever I move a slider.

I'm doing this to change the name of the photo whenever I move a slider that controls slice_selection and sends it in a AJAX GET request.

plt.figure(figsize=(20,10))
plt.imshow(ct_array[slice_selection], clim=(-1000, 3000), cmap='gray')
new_graph_name = "graph_" + str(time.time()) + ".png"
  for filename in os.listdir(pathToStatic):
     if filename.startswith('graph_'):  # not to remove other images
        os.remove(os.path.join(pathToStatic, filename))
plt.savefig(os.path.join(pathToStatic, new_graph_name))

return render_template('lung_view.html', title='Lung cancer view', form=form, graph=new_graph_name)

It works fine, the old file is deleted and the new file has the correct image.

HTML

<img src="" />

After render_template the image doesn't change, even though it is no longer or disk so I tried the following things:

SEND_FILE_MAX_AGE_DEFAULT = 0 in app config

@app.after_request
def after_request(response):
    response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
    response.headers["Expires"] = 0
    response.headers["Pragma"] = "no-cache"
    return response

Disabled cache in F12->Network

Tried using redirect instead of render_template.

Adding dummy ? to src like this:

<img src="?dummy=8484744" />

I don't know what else to do, it feels like nothing is working and I exhausted all the options. Thank you in advance!




Aucun commentaire:

Enregistrer un commentaire