is it able to use CustomJS callback when using bokeh periodic_callback

205 Views Asked by At

I am able to update GeoJSONDataSource using bokeh CustomJS, however, when using periodic_callback, I can't use CustomJS as the callback function. Below is my code with the error message "TypeError: 'CustomJS' object is not callable"

current_year = 1800
n_slider = Slider(start=1, end=50, value=1, step=1, title="N")
toggle = Toggle(label="Play", button_type="success")
callback = CustomJS(args=dict(source=source, year=current_year, n_slider=n_slider, toggle=toggle), code="""
    data = source.data;
    console.log(toggle.active)
    console.log(n_slider.value)
    console.log(year)
    year = year +1
    // edit GeoJSONDataSource
    source.change.emit();
""")

curdoc().add_root(column(p, n_slider, toggle))
curdoc().add_periodic_callback(callback, 1000)

My question is,

Is it possible to use a CustomJS as the callback function of periodic_callback, if not, how can I edit the GeoJSONDataSource entry using the normal python function?

1

There are 1 best solutions below

1
bigreddot On

It is not possible, add_periodic_callback can only run actual Python callbacks (in a Bokeh server application). There is no built-in facility to execute CustomJS callbacks on a set interval. It would be "hacky" but it's possible the AjaxDataSource with its polling interval could be slightlky abused to achieve a similar effect, though.