ZhangZhihui's Blog  

Python code:

import base64
from io import BytesIO


def fig_to_html(fig):
"""
Convert a matplotlib.Figure object to html image source
:param fig:
:return: the returned value can be used in html like this: <img src="data:image/png;base64,<return value>"/>
"""

buf = BytesIO()
fig.savefig(buf, format="png")
figure = base64.b64encode(buf.getbuffer()).decode("ascii")

return figure

 

HTML code:

<img src="data:image/png;base64,{{ figure }}"/>

 

posted on 2022-01-08 16:32  ZhangZhihuiAAA  阅读(30)  评论(0编辑  收藏  举报