在散景中动态添加小部件

Adding widgets dynamically in bokeh

我想在散景中动态添加滤镜,即每次按下按钮时,都会添加一个新滤镜。但是,添加新小部件后布局会被破坏:新小部件会覆盖旧小部件,而不是重新计算布局。代码示例

from bokeh.layouts import row, column
from bokeh.models.widgets import Button, Select
from bokeh.io import curdoc

def add_select():
    feature = Select(value='feat', options=["a"])
    dynamic_col.children.append(feature)

b1 = Button(label="Add condition", button_type="success")
b1.on_click(add_select)

b2 = Button(label="Apply", button_type="success")

dynamic_col = column()
curdoc().add_root(column(b1, dynamic_col, b2))

点击"Add"按钮前的布局

添加 Select 小部件后的布局

为什么不使用一个列表来处理所有小部件?

from bokeh.layouts import column
from bokeh.models.widgets import Button, Select
from bokeh.io import curdoc

def add_select():
    feature = Select(value='feat', options=["a"])
    dynamic_col.children.insert(-1, feature)

b1 = Button(label="Add condition", button_type="success")
b1.on_click(add_select)

b2 = Button(label="Apply", button_type="success")

dynamic_col = column(b1, b2)
curdoc().add_root(dynamic_col)

我 "insert" 而不是 "append" 让第 2 个按钮位于列表末尾的小部件

我得到了这个结果: