我是 Bokeh 的新手,需要一些帮助。我正在尝试根据其他下拉框选择动态更改下拉框 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"
最佳答案
这是一个示例,它将根据在主题区域下拉列表中选择的值更改环境下拉列表中显示的选项。
如果您还想更改值,可以使用相同的方法。
这应该允许您动态更改下拉菜单的值和选项。
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"
关于python - 如何动态更改下拉值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43031964/