I'm setting up a web application in which I need to display some images. The images I receive from the API convert the matplotlib plot to png and then send it to the main web application using the io library. Because of this, the images I display on the page almost always appear incorrect or wrong. But if I open them in a new page via context menu, they work fine.
So this code for sending pictures
@app.route('/send-data-a', methods=['GET']) def send_data_user_dynamic(): ...some code for diagram... image_stream1 = io.BytesIO() plt.savefig(image_stream1, format='png') image_stream1.seek(0) plt.close(fig) return send_file(image_stream1, mimetype='image/png')
I tried putting the timestamp in the main app so the links would be unique but that didn't help
@app.route('/data', methods=['GET']) def data(): timestamp = int(time.time()) user_dynamic = requests.get(f'http://127.0.0.1:5000/send-data-a?timestamp={timestamp}') user_amount = requests.get(f'http://127.0.0.1:5000/send-data-b?timestamp={timestamp}') kp_month = requests.get(f'http://127.0.0.1:5000/send-data-c?timestamp={timestamp}') kp_week = requests.get(f'http://127.0.0.1:5000/send-data-d?timestamp={timestamp}') return render_template('second.html', user_dynamic=user_dynamic.url, user_amount=user_amount.url, kp_month=kp_month.url, kp_week=kp_week.url) if __name__ == '__main__': app.run(debug=True, port=5001)
And there is an html template to output them
<div class="diagram"> <img src="{{ user_dynamic }}" alt="user_dynamic"> <figcaption>some text</figcaption> </div>
So uh this code solves the problem