如何在烧瓶中显示 pydotplus 图

时间:2021-05-29 07:43:30

标签: python-3.x flask pydotplus

我正在尝试在 Flask 应用程序中显示设计树。我正在使用 pydotplus 创建图像,它在我的 jupyter 笔记本中显示良好。但我不确定如何在我的 Flask 应用程序中显示相同的内容。

这是在 jupyter notebook 中显示图表的代码

dot_data = tree.export_graphviz(dt3, out_file=None, feature_names=data_train.columns, impurity=False,
                                    filled=True,
                                    proportion=True,
                                    rounded=True)


graph = pydotplus.graph_from_dot_data(dot_data)
graph.create_png()
image = Image(graph.create_png())
image

1 个答案:

答案 0 :(得分:0)

感谢@hootnot 的指点

这对我有用

dot_data = tree.export_graphviz(dt3, out_file=None, feature_names=data_train.columns, impurity=False,
                                    filled=True,
                                    proportion=True,
                                    rounded=True)

graph = pydotplus.graph_from_dot_data(dot_data)
image = Image(graph.create_png())
Encoded_Image = base64.b64encode(image.data).decode("utf-8")

在烧瓶中

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

基本上需要将png转换为base64编码的字符串,然后传递给flask。 HTML 可以显示 base64 编码的图像字符串。