2017-04-14 22 views
1

私は、BokehとCustomJSをPython関数で使用して、RadioButtonGroupでインタラクティブ機能を使用する方法を理解しようとしています。私はy = x^fをプロットするためにthe example provided at the Bokeh siteを調整しました。パワーfにスライダを使用する代わりに、f = 0.5とf = 0.2という2つのパワーを切り替えたいと思います。私はこのマニュアルを読んで、Jupyterノートブックを使って自分のコードにRadioButtonGroupを挿入しました。ボタンは表示され、応答しますが、ボタンを切り替えることでコールバック応答を得ることができません。はBokeh RadioButtonGroupからの応答を得ることができません

ご協力いただければ幸いです。

from math import pi 
 

 
from bokeh.io import output_file, show 
 
from bokeh.layouts import column 
 
from bokeh.models import ColumnDataSource, CustomJS, Slider, TextInput, RadioButtonGroup 
 
from bokeh.plotting import Figure, output_notebook 
 

 
output_notebook() 
 

 
x = [x*0.005 for x in range(0, 200)] 
 
y = x 
 

 
source = ColumnDataSource(data=dict(x=x, y=y)) 
 

 
plot = Figure(plot_width=400, plot_height=400) 
 
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) 
 

 
def callback(source=source, input1=input1, window=None): 
 
    data = source.data 
 
    m= input1.active 
 
    if m==0: 
 
     f=.5 
 
    else: 
 
     f=2 
 
     
 
    x, y = data['x'], data['y'] 
 
    for i in range(len(x)): 
 
     y[i] = window.Math.pow(x[i], f) 
 
    source.trigger('change') 
 

 
input1 = RadioButtonGroup(labels=["power = .5", "power = 2."], active=0) 
 

 
input1.button_type="success" 
 
input1.js_on_change('active', CustomJS.from_py_func(callback)) 
 

 
layout = column(input1, plot) 
 

 
show(layout)

答えて

0

私はbokeh.models.RadioButtonGroupがコールバックを発生させることはできませんが、bokeh.models.RadioGroupで、私は(私は、新しいバージョンがコールバックを生成するRadioButtonGroupを容易たぶんボケバージョン0.12.4を使用しています)ことができます。また、宣言する前にinput1を参照しています。コールバック関数の入力としては必要ありませんが、内部ではcb_objを使用できます。参照:

import bokeh 
import bokeh.plotting 

bokeh.io.output_notebook() 

x = [x*0.005 for x in range(0, 200)] 
y = x 

source = bokeh.models.ColumnDataSource(data=dict(x=x, y=y)) 

plot = bokeh.plotting.figure(plot_width=400, plot_height=400) 
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6) 

def callback(source=source, window=None): 
    data = source.data 
    m = cb_obj.active 
    if m==0: 
     f=.5 
    else: 
     f=2 

    x, y = data['x'], data['y'] 
    for i in range(len(x)): 
     y[i] = window.Math.pow(x[i], f) 
    source.trigger('change') 

input1 = bokeh.models.RadioGroup(labels=["power = .5", "power = 2."],active=0, 
        callback=bokeh.models.CustomJS.from_py_func(callback)) 

layout = bokeh.layouts.column(input1, plot) 

bokeh.io.show(layout) 

enter image description here

+1

はそれを指摘するためにありがとうございました。ところで、input1宣言の位置を変えて、RadioButtonGroupをBokeh 0.12.5で正常に動作させました。 –

+0

ありがとうございます。 0.12.5に更新する時間。 –

関連する問題