Whisker won't update

Hi, I’m a novice to Bokeh and have been trying to get a whisker plot that can change values on different toggles. While other glyphs (like circles) seem to work just fine, when I try to update glyphs upon changes in my switch object the update doesn’t occur. However, I can set the object’s visibility to false and that will work. Am I misunderstanding something about how Bokeh works?

Thanks
import numpy as np

import bokeh.io as bio
import bokeh.plotting as bp
import bokeh.models as bm
import bokeh.layouts as bl

bio.output_file('test_bokeh_whisker.html')

time_val=np.linspace(0,10,11)
(v1_val,v1_pred)=(np.zeros(11), np.random.randn(11))
(v2_val,v2_pred)=(time_val, time_val+np.random.randn(11))

data_src = bm.ColumnDataSource({
    'rec_t': time_val,
    'v1_val': v1_val,
    'v1_pred': v1_pred,
    'v2_val': v2_val,
    'v2_pred': v2_pred,
})

def ver_switch(attrname, old, new):
    if new=='version 1':
        scatter_whiskers.update(upper='v1_pred', lower='v1_val')
    elif new=='version 2':
        scatter_whiskers.update(upper='v2_pred', lower='v2_val')
#             scatter_whiskers.visible=False

plot = bp.figure(title='Switch Test')

scatter_whiskers = bm.Whisker(source=data_src, base='rec_t', upper='v1_val', lower='v1_pred')
plot.add_layout(scatter_whiskers)
plot.x_range=bm.Range1d(0,max(time_val)*1.02)
plot.y_range=bm.Range1d(-1,11)

ver_select = bm.Select(value='version 1', title='Mode', options=['version 1', 'version 2'])
ver_select.on_change('value', ver_switch)

res = (bl.column(ver_select,plot))

bio.show(res)

@malive show generates standalone output, i.e. pure HTML + JS only. If you want to use real live Python callbacks, e.g. on_change, then you will need to define and run a Bokeh Server Application:

https://docs.bokeh.org/en/latest/docs/user_guide/server.html

The Bokeh server is the Python process that would execute your on_change callback code.

Hi, thanks for getting back to me. I had been using this in Jupyter and ported my code over without adequately that the Circle version of the code worked. After making the changes, I still have the same problem:

import numpy as np

import bokeh.io as bio
import bokeh.plotting as bp
import bokeh.models as bm
import bokeh.layouts as bl

bio.output_file('test_bokeh_whisker.html')

time_val=np.linspace(0,10,11)
(v1_val,v1_pred)=(np.zeros(11), np.random.randn(11))
(v2_val,v2_pred)=(time_val, time_val+np.random.randn(11))

data_src = bm.ColumnDataSource({
    'rec_t': time_val,
    'v1_val': v1_val,
    'v1_pred': v1_pred,
    'v2_val': v2_val,
    'v2_pred': v2_pred,
})

def ver_switch(attrname, old, new):
    if new=='version 1':
        scatter_whiskers.update(upper='v1_pred', lower='v1_val')
    elif new=='version 2':
        scatter_whiskers.update(upper='v2_pred', lower='v2_val')
#             scatter_whiskers.visible=False

plot = bp.figure(title='Switch Test')

scatter_whiskers = bm.Whisker(source=data_src, base='rec_t', upper='v1_val', lower='v1_pred')
plot.add_layout(scatter_whiskers)
plot.x_range=bm.Range1d(0,max(time_val)*1.02)
plot.y_range=bm.Range1d(-1,11)

ver_select = bm.Select(value='version 1', title='Mode', options=['version 1', 'version 2'])
ver_select.on_change('value', ver_switch)

bp.curdoc().add_root(bl.column(ver_select,plot))

doesn’t cause a change in the whisker object, while

import numpy as np

import bokeh.io as bio
import bokeh.plotting as bp
import bokeh.models as bm
import bokeh.layouts as bl

bio.output_file('test_bokeh_circle.html')

time_val=np.linspace(0,10,11)
(v1_val,v1_pred)=(np.zeros(11), np.random.randn(11))
(v2_val,v2_pred)=(time_val, time_val+np.random.randn(11))

data_src = bm.ColumnDataSource({
    'rec_t': time_val,
    'v1_val': v1_val,
    'v1_pred': v1_pred,
    'v2_val': v2_val,
    'v2_pred': v2_pred,
})

def ver_switch(attrname, old, new):
    if new=='version 1':
        scatter_raw_data.update(y='v1_val')
    elif new=='version 2':
        scatter_raw_data.update(y='v2_val')

plot = bp.figure(title='Switch Test')

scatter_raw_data = bm.Circle(x='rec_t', y='v1_val', name='data', size=8, fill_color="#0000ff")
scatter_raw_glyph = plot.add_glyph(source_or_glyph=data_src, glyph=scatter_raw_data)
plot.x_range=bm.Range1d(0,max(time_val)*1.02)
plot.y_range=bm.Range1d(-1,11)

ver_select = bm.Select(value='version 1', title='Mode', options=['version 1', 'version 2'])
ver_select.on_change('value', ver_switch)

bp.curdoc().add_root(bl.column(ver_select,plot))

works as expected.

Thanks for the help!