Converting matplotlib png to base64 for viewing in html template Converting matplotlib png to base64 for viewing in html template flask flask

Converting matplotlib png to base64 for viewing in html template


The beginning of the data in the template gives a clue to what's happening. ' is the HTML entity for a single quote '. Combined with the preceding b, b', it looks like the representation of a byte string, rather than the contents of the string.

Decode the byte string to a string before trying to render them with Jinja.

render_template('result.html', result=figdata_png.decode('utf8'))

Jinja renders the string representation of objects in {{ }}. The string representation of a byte string includes the b'' to distinguish it from a Unicode string. So you have to decode in order to display their value directly.


Try adding the section meta charset="utf-8" into the HEAD section of your template. That worked for me :-)