Hi,
I have a little Bokeh server application where I switch between different plots by changing the root of curdoc(). While that works as intended, there is one persistent problem I could not solve: After removing a model containing an auto-scaling axis from the current document and re-adding it again, the axis no longer scales automatically. I have a minimal example:
import math
import bokeh.io
import bokeh.layouts
import bokeh.models
import bokeh.plotting
def gen_data(start, stop):
“”"
Just some example data
“”"
step = 0.25
steps = [start + i * step for i in range(int((stop - start) / step))]
values = [math.cos(x) for x in steps]
return dict(x=steps, y=values)
def update_source(attr, old, new):
“”"
Callback for Slider: Change the data in the plot
“”"
global source
source.data = gen_data(new[0], new[1])
def go_to_model2():
“”"
Callback for ‘next’-button: Change the current document
“”"
global model2
bokeh.io.curdoc().clear()
bokeh.io.curdoc().add_root(model2)
def restore_model1():
“”"
Callback for ‘previous’-button: Restore the original plot
“”"
global model1
bokeh.io.curdoc().clear()
bokeh.io.curdoc().add_root(model1)
Plot some data
source = bokeh.models.ColumnDataSource(data=gen_data(0, 3))
figure = bokeh.plotting.figure()
renderer = figure.line(source=source, x=‘x’, y=‘y’)
Add a slider to control what data is plotted
slider = bokeh.models.RangeSlider(start=0, end=10, value=(0, 3))
slider.on_change(‘value’, update_source)
A button to switch to the next page/model
next_button = bokeh.models.Button(label=‘Next’)
next_button.on_click(go_to_model2)
``# A button to switch to the previous page/model
`back_button = bokeh.models.Button(label=‘Previous’)
back_button.on_click(restore_model1)
`
model1 = bokeh.layouts.column(next_button, slider, figure)
model2 = bokeh.layouts.column(back_button)
bokeh.io.curdoc().add_root(model1)
``
As you can see, the x-axis of the plot updates automatically if you use the slider. But after using the next
and previous
buttons that is no longer the case. The data gets updated just fine, as you can see by zooming out, but the axis does not change.
How can I solve this issue? I find the auto-scaling quite important for the usability of the document.
I am also not sure if I did the switching between models correctly. I did not find any examples on this topic, thus I made up my own method. I am open to suggestions how to do this better, as I have the suspicion that my current method is the root of my problem.