Skip to content Skip to sidebar Skip to footer

How To Access And Update Bokeh Plots Or Widgets Using An External Javascript Code?

I have a Bokeh plot which is controlled by a time Bokeh slider. I am trying to set the time of the slider and the corresponding plotted data to the current time of the browser by c

Solution 1:

You can embed another JS library in the block postamble part of the Bokeh template like described here. Then if you give your slider a name you can access it like this:

Python:

slider = Slider(start=0, end=10, value=5, name='my_slider')

JS:

var slider = Bokeh.documents[0].get_model_by_name('my_slider')
console.log('slider value before:', slider.value)
slider.value = 10console.log('slider value after:', slider.value)

This is assuming that you have just one Bokeh document in your app (note that 0 index in documents[0]). Then you can access and manipulate the Slider object the same way like you would do in a CustomJS callback, but note that cb_obj and cb_data are not available in this case.

See complete working example below (Bokeh v1.3.0):

external_js.py:

from bokeh.io import save
from bokeh.models import Slider, Column
from bokeh.util.browser import view

template = """
{% block postamble %}
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <script>
        $(document).ready(function() {
            var slider = Bokeh.documents[0].get_model_by_name('my_slider')
            console.log('slider value before:', slider.value)
            slider.value = 10
            console.log('slider value after:', slider.value)
        });
    </script>
{% endblock %}
"""

slider = Slider(start=0, end=10, value=5, name='my_slider')

save(Column(slider), template=template)
view("external_js.html")

Post a Comment for "How To Access And Update Bokeh Plots Or Widgets Using An External Javascript Code?"