Skip to content
Advertisement

How to write custom JS callback in Bokeh (Python)?

I try to build a dynamic chart with Bokeh and I’m sutcked with JavaScript part, wording the Custom JS callback. I precise that I am absolutely not familiar with JavaScript.

Here is my dataframe :

JavaScript

What I am looking for is a chart with two sliders (slider_TRA & slider_TS) based on the values of variables num_tra and num_ts. Finally, I would like to update the sources of the plot depending on the values of the two sliders.

Based on the Bokeh documentation examples, I tried to build the following code, but have no idea how to handle the JS part :

JavaScript

As mentioned above, I’m not famliliar with JS and I’m looking for someone who can help me out. If you have any ideas or suggestions, it would be very appreciated.

Advertisement

Answer

This solution works for Bokeh v2.3.0. You need to pass the complete data to the callback function and make there filtering based on the slider values. But you cannot assign the resulting filtered data to the original data as you will loose information this way. So you should assign the filtered data to the data_source object of the corresponding glyphs. Also the starting data for both lines should get filtered according to initial slider positions.

JavaScript

Result: enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement