问题
I want to add filters dynamically in bokeh, i.e. every time a button is pressed, a new filter is appended. However, the layout gets broken after a new widgets are added: new ones get written over old ones instead of the layout being recomputed. Code example
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))
Layout before clicking "Add" button
Layout after Select widget gets added
回答1:
Why don't you use a single list to handle all your widgets ?
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)
I "insert" instead of "append" the widget to let the 2nd button at the end of the list
I got this result :
来源:https://stackoverflow.com/questions/47820780/adding-widgets-dynamically-in-bokeh