Skip to content
Advertisement

How to define Python Bokeh RangeSlider.on_change callback function to alter IndexFilter for plots?

I’m trying to implement a python callback function for a RangeSlider. The Slider Value should tell which Index a IndexFilter should get for display.

For example: If rangeslider.value is (3, 25) my plots should only contain/view data with the Index from 3 to 25.

JavaScript

Advertisement

Answer

Some notes:

  • time is longer than the rest of the columns – you will receive a warning about it. In my code below, I just removed its last element
  • view with filters in general should not be used for continuous glyphs like lines (v.line in particular – multi_line is fine). You will receive a warning about it. But if the indices in IndexFilter are always continuous, then you should be fine. Either way, you can use the segment glyph to avoid the warning
  • In your callback, you’re trying to set view on the figures – views only exist on glyph renderers
  • In general, you don’t want to recreate views, you want to recreate as few Bokeh models as possible. Ideally, you would have to just change the indices field of the filter. But there’s some missing wiring in Bokeh, so you will have to set the filters field of the view, as below
  • new argument of Python callbacks receives the new value for the attribute passed as the first parameter to the corresponding on_change call. In this case, it will be a tuple, so instead of new.value[0] you should use new[0]
  • Since you’ve decided to use Python callbacks, you can no longer use show and have a static HTML file – you will have to use curdoc().add_root and bokeh serve. The UI needs that Python code to run somewhere in runtime
  • When changing the slider values, you will notice that the separate segments of multi_line will be joined together – it’s a bug and I just created https://github.com/bokeh/bokeh/issues/10589 for it

Here’s a working example:

JavaScript
Advertisement