How to display dtreeviz in dash

Hi.

Does anyone know how to display a dtreeviz result in a dash app?

I tried to save to file, to return the object, to use to_plotly but no luck.
thanks!

Hi @yuriq, could you add some information and/or show us what you have tried so far? I don’t even know, what a dtreeviz is :wink:

Sure thank you!
dtreeviz is a python visulaisation library to present decision trees.
it creates a dot file, using matplot lib - and by default present it as a pop up or in a notebook.
it has an option to save to disk, but you cannot disable the popup;
it also requires an installation of graphviz.

I managed to save an svg, and then read it into dash,
but got multiple warning on multi-thread,

this is the code i used :
# viz_model = dtreeviz.model(
# clf,
# X_train=features,
# y_train=labels,
# feature_names=features.columns,
# target_name=“error”,
# class_names=[“pass”, “fail”],
# )

# v.show()                 # pop up window
# v = viz_model.view()  # render as SVG into internal object
# img_url = "./assets/tmp/dtreeviz_chart.svg"
# v.save(img_url)

eventually i settled for creating a matplot lib image using a more simple library and pass the image data itself to dash html.img
using this snippet:

    fig, ax = plt.subplots(figsize=(15, 10))
    tree.plot_tree(
        clf,
        filled=True,
        ax=ax,
        feature_names=features.columns,
        rounded=False,
        max_depth=max_tree_depth,
        label="all",
        impurity=False,
    )

    # Convert the Matplotlib figure to a PNG image
    buf = io.BytesIO()
    fig.savefig(buf, format="png")
    buf.seek(0)
    img = base64.b64encode(buf.read()).decode("ascii")

return "data:image/png;base64," + img, feature_importances