The analysis result image does not change on the web using the flask

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

I'm making a program that shows the analysis results when I enter a keyword, for example, Celtos, and I show the analysis results. I save the analysis results as an image and spray them on the result page. At first, the analysis results are good, but even if I change the keyword from the second time, I search with the "Celtos" keywordcrying In the development environment, the image is also newly overwritten and stored in a new keyword form, but what is sprayed on the web is the first keyword to store the analyzed image Why is he doing this? ㅠ<

1- Save analysis results

Code: plt.savefig ("static/image/aaa.png")

2 - Send the saved one to result.html

Code: return render_template ("result.html", image_file='static/image/aaa.png' )

I took it in the form of an img in result.html and took it with url_for, but the image doesn't change. ㅠ<

{% if True %}
<div>
    <img src="{{ url_for('static', filename = image_file) }}"/>
</div>
{% {% endif %}
<img src = "../static/image/aaa.png">

flask

2022-09-20 15:01

1 Answers

As Yeopto said, it would be good to encode the file into base64 and spray it on the screen so that it is not cached by the browser because of browser caching.

from flask import Flask, render_template_string
import base64

app = Flask(__name__)

template = '<body> <img src="data:image/png;base64,{{img}}"> </body>'

@app.route('/')
def main():
    with open('0.png', 'rb') as f:
        q = base64.b64encode(f.read())
    return render_template_string(template, img = q.decode())

app.run(port=8008)


2022-09-20 15:01

If you have any answers or tips


© 2022 pinfo. All rights reserved.