2017-03-08 37 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) 
相關問題