I am using matplotlib to render some figure in a web app. I\'ve used fig.savefig() before when I\'m just running scripts. However, I need a function to return a
For Python3 ....
I have a DataFrame, I want to show this plot in Flask ....
So Create a Base64 Image of the plot.
df_week_min_az = pd.DataFrame.from_dict(week_max_az.to_dict(),
orient='index', columns=['min_az'])
sunalt = df_week_max_angle.plot().get_figure()
buf = io.BytesIO()
sunalt.savefig(buf, format='png')
buf.seek(0)
buffer = b''.join(buf)
b2 = base64.b64encode(buffer)
sunalt2=b2.decode('utf-8')
I now call my template using the base64 encoded data like this....
return render_template('where.html', form=form, sunalt=sunalt2)
The relevant part of the template (i.e. the picture bit) looks like this....
{% if sunalt != None %}
Sun Altitude during the year
{% endif %}
Hope that helps someone....