如何动态更改下拉值

时间:2017-03-26 17:14:13

标签: python data-visualization bokeh

我是Bokeh的新手并且需要一些帮助。我正在尝试根据其他下拉2框选择动态更改下拉1框值。我看了Bokeh的例子,但找不到一个。这是我正在搞乱的代码。

source = ColumnDataSource(data=dict(server_list=["old_value_1", "old_value_2"]))

def update():
    tech_val = tech.value
    if tech_val == 'art':
        source.data = dict(
            server_list=["new_value_1", "new_value_2"]
        )
#         servers.update()

env = Select(title="Environment", value="PROD", options=["DEV", "QA", "PROD"])
tech = Select(title="Subject Area", value="science", options=["science", "art"])

servers = Select(title="Server", options=source.data['server_list'])
controls = [env, tech, servers]
for control in controls:
    control.on_change('value', lambda attr, old, new: update())

sizing_mode = 'fixed'

inputs = widgetbox(*controls, sizing_mode=sizing_mode)
l = layout([[inputs]], sizing_mode=sizing_mode)
curdoc().add_root(l)
curdoc().title = "Sliders"

1 个答案:

答案 0 :(得分:4)

下面是一个示例,它将更改“环境”下拉列表中显示的选项,具体取决于在“主题”区域下拉列表中选择的值。

如果您还希望更改值,则可以使用相同的方法。

这应该允许您动态更改下拉列表的值和选项。

from bokeh.layouts import column,row, widgetbox,layout
from bokeh.io import curdoc
from bokeh.models.widgets import (Select)
from bokeh.plotting import ColumnDataSource

source = ColumnDataSource(data=dict(server_list=["old_value_1", "old_value_2"]))

def update(attrname, old, new):
    tval = tech.value
    env.options = env_dict[tval]

tech_options = ["science", "art"]
env_options1  = ["DEV", "QA", "PROD"]
env_options2 = ["DEV2", "QA2", "PROD2"]
env_dict = dict(zip(tech_options,[env_options1, env_options2]))
env = Select(title="Environment", value="PROD", options=["DEV", "QA", "PROD"])
tech = Select(title="Subject Area", value="science", options=tech_options)

servers = Select(title="Server", options=source.data['server_list'])

""" update drop down 1 based off drop down 2 values """
tech.on_change("value", update)

sizing_mode = 'fixed'

inputs = widgetbox([env,tech,servers], sizing_mode=sizing_mode)
l = layout([[inputs]], sizing_mode=sizing_mode)
curdoc().add_root(l)
curdoc().title = "Sliders"
相关问题