Reputation: 473
I'd like to be able to change things about the slider (the value, the start/end values) programmatically.
So I take the standard slider.py demo, and just add this at the end:
for i in range(5):
amp_slider.value = amp_slider.value + 1
time.sleep(1)
That should move the value upwards every second for a few seconds. But the slider doesn't move. What am I doing wrong? Or similarly if I try to change the .end or .start value.
[I know sliders are supposed to be INPUT not OUTPUT devices. But nonetheless I'm trying to control its behavior.]
Upvotes: 2
Views: 1814
Reputation: 11
I realize this question is six years old but it still comes up on a search for this topic, so... you can programmatically update a Bokeh slider using Python if you create a server program and host it with 'bokeh serve --show your_app.py'.
For example, given a RangeSlider object these callback functions change the slider start and end values:
def _update_slider_start(self, attr, old, new):
new_value = list(self.slider.value)
new_value[0] = new
new_value = tuple(new_value)
self.slider.update(start=0.0, end=1.0, value=new_value)
def _update_slider_end(self, attr, old, new):
new_value = list(self.slider.value)
new_value[1] = new
new_value = tuple(new_value)
self.slider.update(start=0.0, end=1.0, value=new_value)
in this example the Bokeh RangeSlider object is a member of a class and referenced as self.slider. My range was fixed between 0 and 1 but that is easily changed.
Then in my use case I invoke these callback functions when the range of a plot changes:
self.plot.y_range.on_change('start', self._update_slider_start)
self.plot.y_range.on_change('end', self._update_slider_end)
I also started with a sample from the Bokeh site that has a slider changing the axis range of a plot. However, when the plot is zoomed it doesn't change the slider. That leads to a difficult user interface where using zoom and the slider together causes the chart range to jump around. This code snippet allows the slider to make incremental adjustment to the plot axis after the plot is zoomed.
Upvotes: 0
Reputation: 36
The only code inside your program that will be used again once the page is created is in the callback functions. If you adjust sliders.py so it reads:
def update_title(attrname, old, new):
amplitude.value += 1
Every time you update the text, the amplitude will increase.
Upvotes: 0
Reputation: 11070
bokeh show()
outputs the chart as html & javascript. Once it has done this it can no longer be modified (unless you wrote some javascript which was included to modify the page).
You need a library that renders in a 'dynamic' window (such as matplotlib
to be able to replot a chart like this.
Upvotes: 2