Skip to main content
 首页 » 编程设计

python之无法清除 Bokeh 服务器中的文档

2025年02月15日13zhenyulu

这是我用来清除 Bokeh 服务器中高级图表的示例代码。我想在通过点击按钮清除旧情节后添加一个新的高级情节,但我无法清除旧情节。 Sample screenshot

from bokeh.charts import  Bar 
from bokeh.layouts import column 
from bokeh.models.widgets import Button 
from bokeh.plotting import  curdoc 
 
from bokeh.sampledata.autompg import autompg as df 
 
button = Button(label="Clear") 
 
def clear(): 
    curdoc().clear() 
 
p = Bar(df, 'cyl', values='mpg',  
        title="Total MPG by cyl") 
 
button.on_click(clear) 
curdoc().add_root(column(button,p) ) 

请您参考如下方法:

尝试以这种方式完全清除文档可能会出现问题。当前最好的做法是使文档的顶层具有某种布局(例如 行 或 列),然后更新该布局的 child 。一般来说,从 Bokeh 0.12.3 开始,更新 东西比 替换 东西更可靠可能是正确的。

您可以在交叉过滤器示例中看到这样的示例:

https://github.com/bokeh/bokeh/blob/master/examples/app/crossfilter/main.py#L72

它在哪里使用这样的代码:

def update(attr, old, new): 
    layout.children[1] = create_figure()