2017-03-08 39 views
0

一个如何可以通过在一个小部件获得的另一个值?例如,假设我有两个功能一个控件的值传递给另一小窗口的背景虚化

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) 

提前致谢!

+0

您可以提供这背后更多的代码?但是,应该可以获得一个小部件的价值并将其传递给另一个小部件。 – Anthonydouc

+0

@Okonomiyaki,感谢您在过去几天的帮助。在网上很难找到这些东西的好例子。当前版本的代码在这里: http://pastebin.com/b9Zs53jj 我无法将两个输入文件上传到github,因为它们太大了,但如果你需要它们,请告诉我。代码中还有一个额外的文本框小部件,它目前不做任何事情,但将在未来 - 所以只是忽略它。再次感谢! – Kyle

+0

如果我的答案对您有帮助并且是正确的,您是否可以赞扬并标记为正确。谢谢 :) – Anthonydouc

回答

2

下面是包含两个数据集的例子,需要设置各个要成为源。第一个按钮只给你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) 
相关问题