将一个小部件值传递给 Bokeh 中的另一个小部件

Passing one widget value to another widget in Bokeh

如何将一个小部件中获取的值传递给另一个小部件?例如,假设我有两个函数

def update_dataset(attrname, old, new):
    dataset = dataset_select.value
    n_samples = int(samples_slider.value)

    data1 = get_dataset(dataset, n_samples)

def update_slider(attrname, old, new):
    n_samples = int(samples_slider.value)

    data2 = get_ends(data1, n_samples)
    source_chart.data = dict(x=data2['aspects'].tolist(), y=data2['importance'].values)

第一个函数(update_dataset)获取了一个新的数据集,我想将这个数据集传递给第二个函数(update_slider)以在

行中使用
data2 = get_ends(data1, n_samples)

提前致谢!

这里有两个数据集的示例,您需要将每个数据集设置为一个源。第一个按钮只是给你 x 和 y = 每个随机数的列表。第二个按钮然后从 0 到 max(x)、max(y) 中随机抽样并绘制它。 希望这能为您提供执行所需操作的模板?

import random
from bokeh.layouts import layout
from bokeh.io import curdoc
from bokeh.models.widgets import Button
from bokeh.plotting import figure, ColumnDataSource



""" widget updating function """

def update_button1():
    # get random data on source 1
    newdata = data_reform()
    source1.data = newdata

def update_button2():
    # allocates source 2 data to data 1
    data = source1.data
    newdata2 = expand_data(data)
    source2.data = newdata2

""" create buttons and allocate call back functions """
button1 = Button(label="Press here to update source1", button_type="success")
button2 = Button(label="Press here to update source2 from source1 data",
                 button_type="success")
button1.on_click(update_button1)
button2.on_click(update_button2)    

""" example functions that operate on our data """
def data_reform():
    newdata = {'x':[random.randint(40,100)]*10, 'y':[random.randint(4,100)]*10}
    return newdata

def expand_data(data):
    max_x = max(data['x'])
    max_y = max(data['y'])
    if(max_x <20):
        max_x = 20
    if(max_y <20):
        max_y = 20
    data = {'x':random.sample(range(0,max_x),20),
                 'y':random.sample(range(0,max_y),20)}
    return data

source1 = ColumnDataSource({'x':[40]*10,'y':[30]*10})
source2 = ColumnDataSource({'x':[0]*20,'y':[20]*20})

""" example plots to show data changing """
figure1 = figure(plot_width=250,
             plot_height=200,
             x_axis_label='x',
             y_axis_label='y')
figure2 = figure(plot_width=250,
             plot_height=200,
             x_axis_label='x',
             y_axis_label='y')
figure1.vbar(x='x', width=0.5, bottom=0,top='y',source=source1,
             color="firebrick")
figure2.vbar(x='x', width=0.5, bottom=0,top='y',source=source2,
             color="firebrick")

layout = layout([[figure1, figure2, button1, button2]])
curdoc().add_root(layout)