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

时间:2017-03-08 03:48:47

标签: python 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)

提前致谢!

1 个答案:

答案 0 :(得分:2)

以下是两个数据集的示例,您需要将每个数据集设置为源。第一个按钮只给你x和y =每个随机数的列表。然后第二个按钮从0到最大(x),最大(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)