0

I am trying to display a design tree in a flask app. I am using pydotplus to create the image and it shows fine in my jupyter notebook. But I am not sure how to display the same in my flask application.

Here's the code to display the graph in 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
pinaki
  • 5,393
  • 2
  • 24
  • 32
  • 1
    I use io.Bytes() for some maptplotlib imaging in Flask. Check: https://stackoverflow.com/questions/11017466/flask-to-return-image-stored-in-database – hootnot May 29 '21 at 07:55
  • thanks @hootnot, I was able to figure it out from that link. Posted as an answer for anyone who comes on this path – pinaki May 29 '21 at 08:16

1 Answers1

1

Thanks @hootnot for the pointer

This worked for me

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")

In flask

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

Basically need to convert the png to base64 encoded string and then pass to flask. HTML can display base64 encoded image strings.

pinaki
  • 5,393
  • 2
  • 24
  • 32