How to create dynamic plots to display on Flask? : <p>I wish to create dynamic plots based on user input on a flask app. However I am getting the following error: 
string argument expected, got 'bytes'</p>

<p>If I use io.BytesIO(), I am not getting this error, but I am not getting the plot on test.html</p>

<pre><code>from flask import Flask
from flask import render_template
import matplotlib.pyplot as plt
import io
import base64

app = Flask(__name__)

@app.route('/plot')
def build_plot():
    img = io.StringIO()
    y = [1,2,3,4,5]
    x = [0,2,1,3,4]
    plt.plot(x,y)
    plt.savefig(img, format='png')
    img.seek(0)

    plot_url = base64.b64encode(img.getvalue())
    return render_template('test.html', plot_url=plot_url)

if __name__ == '__main__':
    app.debug = True
    app.run()
</code></pre>

<p>Test.html</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;title&gt; Plot&lt;/title&gt;
&lt;body&gt;
&lt;img src="data:image/png;base64, {{ plot_url }}"&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
