散景中相互依赖的小部件
interdependent widgets in bokeh
在 bokeh
中,我想根据另一个 Select
小部件中的所选值调整一个 Select
小部件中的可能选项。我的最低限度无效示例如下所示:
from bokeh.io import output_notebook, show
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Select
output_notebook()
# data source
foods = {'fruit': ['apple', 'orange', 'cherry'],
'veg': ['carrot', 'celery']}
source = ColumnDataSource(data=foods)
def change_options_in_choice2(source=source):
'''this is probably the place for magic'''
f = cb_obj.get('value')
print(f)
# first choice
choice1 = Select(title="food group:", value='fruit',
options=list(foods.keys()),
callback=CustomJS.from_py_func(change_options_in_choice2))
# options for second choice depend on choice in first choice
choice2 = Select(title='food items:', value='apple',
options=foods['fruit'])
# merge them
show(column(choice1, choice2))
事实上,我只能在苹果、橙子或樱桃中选择我的食物,即使我换了食物分组到 veg。不知何故,我希望我可以使用 choice1
中的回调更新 choice2
中的可能选择。我该怎么做?
受 Bryan (bigreddot) 评论的启发,我成功地尝试了这个。可以搭配 bokeh serve main.py
'''
with inspiration from
https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/main.py
'''
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models.widgets import Select
# data source
foods = {'fruit': ['apple', 'orange', 'cherry'],
'veg': ['carrot', 'celery']}
def change_options_in_choice2(attrname, old, new):
'''this is probably the place for magic'''
choice2.options = foods[new]
# first choice
choice1 = Select(title="food group:", value='fruit',
options=list(foods.keys()))
choice1.on_change('value', change_options_in_choice2)
# options for second choice depend on choice in first choice
choice2 = Select(title='food items:', value='apple',
options=foods['fruit'])
widgets = column(choice1, choice2)
# initialize
curdoc().add_root(widgets)
curdoc().title = "Eat healthy!"
在 bokeh
中,我想根据另一个 Select
小部件中的所选值调整一个 Select
小部件中的可能选项。我的最低限度无效示例如下所示:
from bokeh.io import output_notebook, show
from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource
from bokeh.models.widgets import Select
output_notebook()
# data source
foods = {'fruit': ['apple', 'orange', 'cherry'],
'veg': ['carrot', 'celery']}
source = ColumnDataSource(data=foods)
def change_options_in_choice2(source=source):
'''this is probably the place for magic'''
f = cb_obj.get('value')
print(f)
# first choice
choice1 = Select(title="food group:", value='fruit',
options=list(foods.keys()),
callback=CustomJS.from_py_func(change_options_in_choice2))
# options for second choice depend on choice in first choice
choice2 = Select(title='food items:', value='apple',
options=foods['fruit'])
# merge them
show(column(choice1, choice2))
事实上,我只能在苹果、橙子或樱桃中选择我的食物,即使我换了食物分组到 veg。不知何故,我希望我可以使用 choice1
中的回调更新 choice2
中的可能选择。我该怎么做?
受 Bryan (bigreddot) 评论的启发,我成功地尝试了这个。可以搭配 bokeh serve main.py
'''
with inspiration from
https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/main.py
'''
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models.widgets import Select
# data source
foods = {'fruit': ['apple', 'orange', 'cherry'],
'veg': ['carrot', 'celery']}
def change_options_in_choice2(attrname, old, new):
'''this is probably the place for magic'''
choice2.options = foods[new]
# first choice
choice1 = Select(title="food group:", value='fruit',
options=list(foods.keys()))
choice1.on_change('value', change_options_in_choice2)
# options for second choice depend on choice in first choice
choice2 = Select(title='food items:', value='apple',
options=foods['fruit'])
widgets = column(choice1, choice2)
# initialize
curdoc().add_root(widgets)
curdoc().title = "Eat healthy!"