pythonbokehbokehjs

linking JS bokeh widget


I have two simple bokeh widgets: a Select and a Slider - I can get the two widgets working separately but I can't find a way to get the two widgets linked together - so that the js_on_change action on one of them will update the status of the other.

My attempt below fails at the linking stage:


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

output_notebook(INLINE) 

options = ['a', 'b', 'c']
indexes = [0, 1, 2]

s1 = ColumnDataSource(data=dict(options=options))
s2 = ColumnDataSource(data=dict(indexes=indexes))


select = Select(title="Option:", options=options)
slider = Slider(title="Index", value=0, start=0, end=len(indexes) -1, step=1)

select_callback = CustomJS(args=dict(options=s1, indexes=s2), code="""
var opt = options.data;
console.log(cb_obj.value, 
            Object.values(options.data)[0].indexOf(cb_obj.value));""")

slider_callback = CustomJS(args=dict(options=s1, indexes=s2, select=select), code="""
var opt = options.data;
console.log(Object.values(opt)[0][cb_obj.value], 
            cb_obj.value);""")

select.js_on_change('value', select_callback)
slider.js_on_change('value', slider_callback)

# the following will not work as I am not using it properly
# slider.js_link('value', select, 'value')

show(column(select, slider))

I need to have this behavior running on the JS code, as for my use case, I need to embed the resulting widgets in a static HTML page (no bokeh-server).

Thanks for any advice!


Solution

  • from bokeh.io import show
    from bokeh.layouts import column
    from bokeh.models import CustomJS, Select
    from bokeh.models.widgets import Slider
    
    options = ['a', 'b', 'c']
    
    init_idx = 0
    select = Select(title="Option:", options=options, value=options[init_idx])
    slider = Slider(title="Index", value=init_idx, start=0, end=len(options) - 1, step=1)
    
    select.js_on_change('value', CustomJS(args=dict(slider=slider),
                                          code="slider.value = cb_obj.options.indexOf(cb_obj.value);"))
    slider.js_on_change('value', CustomJS(args=dict(select=select),
                                          code="select.value = select.options[cb_obj.value];"))
    
    show(column(select, slider))