2017-03-26 72 views
0

我是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

下面是一个示例,它将根据“主题”区域下拉列表中选择的值更改“环境”下拉列表中显示的选项。

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

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

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"