Python 创建保存png按钮到ipywidgets

Python 创建保存png按钮到ipywidgets,python,tree,ipywidgets,Python,Tree,Ipywidgets,我有一个函数可以将我的分类树显示为IPyWidget,如下所示: labels = X.columns #functions to create and fit the decision tree def plot_tree(criterion, depth, split, min_split, min_leaf=0.2): #create tree clf = DecisionTreeClassifier(criterion = criterion, max_depth =

我有一个函数可以将我的分类树显示为IPyWidget,如下所示:

labels = X.columns

#functions to create and fit the decision tree
def plot_tree(criterion, depth, split, min_split, min_leaf=0.2):
    #create tree
    clf = DecisionTreeClassifier(criterion = criterion, max_depth = depth, 
                                       splitter = split, min_samples_split=min_split, min_samples_leaf=min_leaf)
    clf.fit(X, y)

    #create graph
    graph = Source(tree.export_graphviz(clf, out_file=None, feature_names=labels, filled=True))

    display(SVG(graph.pipe(format='svg')))

    return clf

#create buttons for interactive graph
inter = interactive(plot_tree, criterion=["gini", "entropy"], depth=np.arange(1, 12),
                   split = ["best", "random"], min_split=(0.1,1), min_leaf=(0.1,0.5))

display(inter)
还有一种将树保存为png的方法:

png_bytes = graph.pipe(format='png')
with open('../graph/tree.png','wb') as f:
    f.write(png_bytes)
Image(png_bytes);
问题是,如果我将save part作为函数放在“def plot_tree”之后,我将得到png作为第一个显示,树的深度只有1。如果我在另一个单元格中添加保存部分,我将保存整个树


我想向小部件添加一个按钮,该按钮将保存显示树,并显示所有参数集。我该怎么做呢?

你试过把它放在
绘图树中吗?
?是的。。。这是第一种情况。在plot_tree内部,它只保存树的第一个深度,因为它是ipywidgets加载的第一个视图