Dtreeviz on Streamlit

I tried dtreeviz with streamlit, but the decision tree is not displayed using st.write () or st.image ().
Has anyone tried dtreeviz and made it work?

Hi @59er, welcome to the Streamlit community!

In reading the dtreeviz docs briefly, it looks like it might output a graphviz chart. Can you try st.graphviz_chart(fig) and see if that gets you what you want?

https://docs.streamlit.io/en/stable/api.html?highlight=graphviz#streamlit.graphviz_chart

Best,
Randy

Thank you very much for your quick response. I tried st.graphviz_chart(fig) but I got the following error:

StreamlitAPIException : Unhandled type for graphviz chart: <class ‘dtreeviz.trees.DTreeViz’>

my cording is as follows:

from sklearn.datasets import make_moons

from sklearn.tree import DecisionTreeClassifier
from dtreeviz.trees import dtreeviz
import streamlit as st
import graphviz as graphviz
X, y = make_moons(n_samples=20, noise=0.25, random_state=3)
treeclf = DecisionTreeClassifier(random_state=0)
treeclf.fit(X, y)
viz= dtreeviz(treeclf, X, y, target_name="Classes",
    feature_names=["f0", "f1"], class_names=["c0", "c1"])
st.graphviz_chart(viz)

I would appreciate it if you could give me some advice.

Thank you.

Looks like this will do it:

st.image(viz._repr_svg_(), use_column_width=True)

The use_column_width argument is optional, but it will scale the chart responsively relative to what else is in your app, so I tend to use it for most things.

Best,
Randy

Thank you very much for your quick response.

I used “st.image(viz.repr_svg(), use_column_width=True)” and I got the same decision tree image without any fonts.

Do you have any idea to fix this fonts problem?

The image with fonts are displayed on Jupyter Notebook but are not displayed with streamlit.

I tried to clear matplotlib cache (fontlist-v330.json) and add the following:

plt.rcParams[“font.family”] = “Arial”

But still does not work about fonts.

Thank you.

Unfortunately, I don’t know. As it is, I’m just calling the SVG method from the Dtreeviz object, so I would’ve expected that it would handle any of the font options properly. Maybe open an issue on their repository?

Hello!
I am not sure why it was resolved, but the font issue was solved with the coding below (using ‘streamlit.components.v1’).
Anyway, thank you very much for your advice.

from sklearn.datasets import make_moons
from sklearn.tree import DecisionTreeClassifier
from dtreeviz.trees import dtreeviz
import streamlit as st
import graphviz as graphviz
import matplotlib.pyplot as plt
import streamlit.components.v1 as components

st.set_option('deprecation.showPyplotGlobalUse', False)

X, y = make_moons(n_samples=20, noise=0.25, random_state=3)

treeclf = DecisionTreeClassifier(random_state=0)
treeclf.fit(X, y)

viz= dtreeviz(treeclf, X, y, target_name="Classes",

    feature_names=["f0", "f1"], class_names=["c0", "c1"])

def st_dtree(plot, height=None):

    dtree_html = f"<body>{viz.svg()}</body>"

    components.html(dtree_html, height=height)

st_dtree(dtreeviz(treeclf, X, y, target_name="Classes",feature_names=["f0", "f1"], class_names=["c0", "c1"]),800)

Thank you.