2016-05-03 17 views
1

私は、3次元以上のシミュレーションの結果である多くのプロットを滑りたいと思っています。私はPython経由でBokehパッケージを使用しています。Bokehの従属スライダー、コールバックの書き方

簡略化のため、dncという2つのディメンションがあるとします。しかし、NCは次のようにDに依存します。

if d=100, nc=56,57 
if d=20, nc=5,6 

そして私は4枚持っている:

d_100_nc_56.png, 
d_100_nc_57.png, 
d_20_nc_5.png, 
d_20_nc_6.png 

だから私は2つのスライダー、Dに1つ、およびNCのための1つをしたい、としBokeh.plotting.Figureのimage_url関数を使用して.pngイメージを繰り返します。しかし、私はD

from bokeh.io import vform 
from bokeh.models import CustomJS, ColumnDataSource, Slider 
from bokeh.plotting import Figure, output_file, show 

output_file('image.html') 

source = ColumnDataSource(data=dict(url=['d_100_nc_55.png'])) 

p = Figure(x_range=(0,1), y_range=(0,1)) 

callback_nc = CustomJS(args=dict(source=source), code=""" 
    var data = source.get('data'); 
    var f = cb_obj.get('value') 
    old = data['url'][0] 
    to_replace=old.substring(old.lastIndexOf("nc_")+3,old.lastIndexOf(".png")) 

    data['url'][0] = old.replace(to_replace,f.toString(10)) 
    source.trigger('change'); 
""") 

callback_d = CustomJS(args=dict(source=source), code=""" 
    var data = source.get('data'); 
    var f = cb_obj.get('value') 
    old = data['url'][0] 
    to_replace=old.substring(old.lastIndexOf("d_")+2,old.lastIndexOf("_nc_")) 

    data['url'][0] = old.replace(to_replace,f.toString(10)) 
    source.trigger('change'); 
""") 

p.image_url('url',source=source, x=0, y=1,w=1,h=1) 
p.text(x=0,y=0,text=source.data['url']) 
slider_nc = Slider(start=55, end=65, value=1, step=1, title="nc", callback=callback_nc) 
slider_d = Slider(start=20, end=100, value=100, step=80, title="density", callback=callback_d) 

layout = vform(slider_nc,slider_d, p) 
show(layout) 

にスライダーを変更するとNCスライダは自身を更新する必要がありますの値は、しかし、私はNCへの引数としてDスライダーを渡す方法がわかりませんスライダを使用してプロパティを取得し、そのプロパティを即座に更新します。これは可能ですか?さもなければ、ボケを介して複数のスライダの使用をかなり制限します。

答えて

5

編集:最近のバージョン

あなたはargs辞書にアイテムとして、スライダーあなたがsourceを渡すのと同じ方法を渡すために更新。そこを通るPython側のBokehモデルは自動的にコールバックで利用可能になります。その後、BokehJSモデルのプロパティは、まさにここreference guideで説明したPythonのプロパティと一致する別のオフに基づいて1つのスライダーを更新例です。

# Example from Bokeh 0.12.x 

from bokeh.plotting import show, output_file 
from bokeh.layouts import column 
from bokeh.models import CustomJS, Slider 

s1 = Slider(start=1, end=10, value=1, step=1) 
s2 = Slider(start=0, end=1, value=0, step=1) 

s1.callback = CustomJS(args=dict(s1=s1, s2=s2), code=""" 
    s2.end = s1.value; 
""") 

output_file("foo.html") 

show(column(s1,s2)) 
+0

ありがとうございました。これは素晴らしいです ! – Mathusalem

+0

'CustomJS.from_py_func(callback)'のようにPython関数に相当するものは何でしょうか? – Matt

+0

この場合、更新された(より単純な)JSコードと同じです。個人的に、私は 'from_py_func'が好きではありません – bigreddot

関連する問題