2017-03-08 11 views
0

1つのウィジェットで得られた値を別のウィジェットに渡すにはどうすればよいですか?たとえば、私は2つの機能Bokehの別のウィジェットに1つのウィジェット値を渡す

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

@お小の宮キ、ここ数日、あなたの助けてくれてありがとう。これらの種類のものの良い例をオンラインで見つけるのは難しいようです。現在のバージョンのコードは http://pastebin.com/b9Zs53jj githubには2つの入力ファイルをアップロードできません。サイズが大きすぎるため、必要な場合はお知らせください。コードには現在何もしていないテキストボックスウィジェットが追加されていますが、将来はそれが無視されます。再度、感謝します! – Kyle

+0

私の回答があなたに役立っていて正しかった場合は、アップフォートして正しいとマークできますか?ありがとう:) – Anthonydouc

答えて

2

ここでは2つのデータセットの例を示します。それぞれをソースに設定する必要があります。最初のボタンはxとy = 1つの乱数のリストを与えるだけです。 2番目のボタンは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) 
関連する問題