如何刷新Bokeh文档

10

我想要刷新一个Bokeh文档,以便我可以用新的图表替换旧的。但是现在,我只会得到新的图表添加到文档中,所以旧的图表不会消失。

#myfile.py
from bokeh.plotting import curdoc, figure
doc = curdoc()
p1 = figure(width=1500, height=230, active_scroll="wheel_zoom")
doc.add_root(p1)
doc.clear()
p2 = figure(width=1500, height=500, active_scroll="wheel_zoom")
doc.add_root(p2)

这会导致第二个图在第一个图之后显示,但我想要的预期行为是第二个图替换第一个图。我该如何解决?我是通过使用bokeh serve --show myfile.py在bokeh服务器上运行它的。

2个回答

13

达成此类目标的最佳方法是拥有某种顶级布局(例如rowcolumn),其中包含您想要替换的内容。然后,当您想要替换内容时,请保留布局容器,但更改其children属性的值:

from bokeh.plotting import curdoc, figure
from bokeh.layouts import row

doc = curdoc()

p1 = figure(width=1500, height=230, active_scroll="wheel_zoom")

layout = row(p1)
doc.add_root(layout)

p2 = figure(width=1500, height=500, active_scroll="wheel_zoom")

layout.children[0] = p2
您可以在Crossfilter示例中看到类似的技术。

7

如果有人在处理多个元素(例如小部件、更多的图形、行等)时,不知道如何设置布局中的子元素,可以通过将这些元素放入一个布局中,并直接指定children属性来实现:

p2 = figure(width=1500, height=500, active_scroll="wheel_zoom")
p3 = figure(width=1500, height=500, active_scroll="wheel_zoom")
new_layout = row(p2, p3)
layout.children = new_layout.children

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接