Adding widgets dynamically in bokeh

∥☆過路亽.° 提交于 2019-12-10 20:26:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!