2016-07-06 11 views
0

In bokeh möchte ich die möglichen Optionen in einem Select Widget abhängig von dem gewählten Wert in einem anderen Select Widget anpassen. Meine minimal nicht-funktionierendes Beispiel sieht wie folgt aus:Interdependent-Widgets in 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)) 

Wie es ist, kann ich nur wählen unter Äpfel, Orangen oder Kirschen für meine Lebensmittel, auch wenn ich das Essen Gruppe veg wechseln. Irgendwie hoffe ich, dass ich die möglichen Wahlen in choice2 mit einem Rückruf in choice1 aktualisieren kann. Wie würde ich das tun?

+1

serviert werden Setzen Sie '.options' auf eine neue Liste von Optionen. Hier ist ein App-Beispiel, das es in Python macht: https://github.com/bokeh/bokeh/blob/master/examples/app/stocks/main.py#L99-L105, aber der Prinzipal ist der selbe für einen CustomJS Callback . – bigreddot

+0

Danke - das hat bei mir funktioniert. Ich weiß nicht, wie man das in CustomJS macht, aber die Bokeh-Server-Lösung ist für meine Zwecke in Ordnung. – DrSAR

Antwort

1

Inspiriert von Bryan (Bigreddot) Kommentar ich habe es erfolgreich versucht. Es kann mit 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!"