python - 如何动态更改下拉值

标签 python data-visualization bokeh

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

相关文章:

python - 单击 : Use another function in chained commands with context object

jquery - 如何为列表中的每个项目创建一个唯一的 Jquery 对话框?

python - 从两个可变长度字符串数组返回相似度矩阵(scipy 选项?)

javascript - 如何将 Highcharts 折线图从简单的 html 转移到 Flask?

javascript - 何时在 d3 中使用持续时间与延迟

python - Bokeh 图为空

python - 列表中的词典

python - 如何制作这些连续直方图/密度估计图

google-chrome - 无法选择 Bokeh 工具

python - Bokeh :列数据源部分给出错误