I am trying to plot some values and pass them to a Flask template. I tried using this method: How to render and return plot to view in flask?
Unfortunately I get this error.
TypeError: string argument expected, got 'bytes'
I got through to the template when changed the img type to BytesIO, but still it wouldn't show the plot.
I am dead in the water here, can anybody help?
Here is my plot.py
@app.route('/plot', methods=['POST']) # I use POST because I will introduce user specified data
def plot():
if request.method == 'POST':
img = io.BytesIO()
x = [1,2,3,6,7]
y = [4,6,7,9,10]
plt.plot(x, y)
plt.savefig(img, format='png')
plt.savefig("../xxxx.png") # this works just fine
img.seek(0)
plot_url = base64.b64encode(img.getvalue())
return render_template('plot.html', plot_url=plot_url)
And this is plot.html:
{% extends "layout.html" %}
{% block body %}
<img src="data:image/png;base64, {{ plot_url }}" width="20" height="20" alt="graph">
{% endblock %}