2016-07-06 4 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'를 설정하십시오. 다음은 파이썬에서이를 수행하는 App 예제입니다 : https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/main.py#L99-L105하지만 Principal은 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!" 
관련 문제