2016-07-06 14 views
0

Selectウィジェットの選択した値に応じて、Selectウィジェットの可能なオプションを調整したいと思います。マイ最小限に動作していない例は以下のようになります。そのままbokehの相互依存ウィジェット

from bokeh.io import output_notebook, show 
from bokeh.layouts import column 
from bokeh.models import CustomJS, ColumnDataSource 
from bokeh.models.widgets import Select 

output_notebook() 

# data source 
foods = {'fruit': ['apple', 'orange', 'cherry'], 
     'veg': ['carrot', 'celery']} 

source = ColumnDataSource(data=foods) 

def change_options_in_choice2(source=source): 
    '''this is probably the place for magic''' 
    f = cb_obj.get('value') 
    print(f) 

# first choice 
choice1 = Select(title="food group:", value='fruit', 
       options=list(foods.keys()), 
       callback=CustomJS.from_py_func(change_options_in_choice2)) 

# options for second choice depend on choice in first choice 
choice2 = Select(title='food items:', value='apple', 
       options=foods['fruit']) 


# merge them 
show(column(choice1, choice2)) 

、私は私がベジタリアンに食品群を切り替えてもリンゴ、オレンジ、または私の食品用チェリーの間で選択することができます。どういうわけか、私はchoice2の可能な選択を、choice1のコールバックを使って更新できることを願っています。どうすればいい?

+1

オプションの新しいリストに '.options'を設定してください。これは、Pythonでそれを行うアプリケーションの例です:https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/main.py#L99-L105しかし、プリンシパルはCustomJSコールバックで同じです。 – bigreddot

+0

ありがとう - それは私のために働いた。私はCustomJSでこれを行う方法がわかりませんが、私の目的のためにはbokehサーバーの解決策は問題ありません。 – DrSAR

答えて

1

Bryan's(bigreddot's)のコメントに触発されました。私はこれをうまく試しました。 bokeh serve main.py

''' 
with inspiration from 
https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/main.py 
''' 
from bokeh.io import curdoc 
from bokeh.layouts import column 
from bokeh.models.widgets import Select 
# data source 
foods = {'fruit': ['apple', 'orange', 'cherry'], 
     'veg': ['carrot', 'celery']} 
def change_options_in_choice2(attrname, old, new): 
    '''this is probably the place for magic''' 
    choice2.options = foods[new] 
# first choice 
choice1 = Select(title="food group:", value='fruit', 
       options=list(foods.keys())) 
choice1.on_change('value', change_options_in_choice2) 
# options for second choice depend on choice in first choice 
choice2 = Select(title='food items:', value='apple', 
       options=foods['fruit']) 
widgets = column(choice1, choice2) 
# initialize 
curdoc().add_root(widgets) 
curdoc().title = "Eat healthy!" 
関連する問題