On_change Callbacks

Hello,

I am introducing myself to callbacks, and I have a simple example here that is giving a weird behavior to what I expected.

After launching the server, I select a point or many points, and the circles corrected update their radius. However, nothing happens to the line_width or color. However, if I select other points, those two changes take effect.

Why is that? I expected the three changes to happen at the same time.

Thanks in advance.

TOOLS = ["tap", 'poly_select','box_select', 'reset']
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(x = [1, 2, 3, 4, 5, 6, 7, 8, 9],
                               y = [11, 12, 13, 15, 15, 11, 12, 11, 15]))
circle = p.circle(x='x', y='y', source=source)


def callback(attr, old, new):
    print('here')
    print(old, new)
    circle.glyph.fill_color = 'green'
    circle.glyph.radius = 3
    circle.glyph.line_width = 2

source.selected.on_change('indices', callback)

curdoc().add_root(column(p))

@LorenzoPeve I think it would be more helpful if you actually describe what you want to have happen. When there are selections involved, there is more than just .glyph, there are additional .selection_glyph and .nonselection_glyph that control the appearance of selected and non-selected points respectively. Depending on what you actually want to accomplish, you would need to update those as well.

But more likely, you wouldn’t need to update anything in a callback. If all you want to do is control the appearance of selected vs non-selected points, that is typically a configuration, not requiring any callback code. See:

https://docs.bokeh.org/en/2.4.3/docs/user_guide/styling.html#selected-and-unselected-glyphs

(note the hard-versioned link to 2.4.3 docs… the user’s guide is about to be completely restructured for upcoming 3.0 release)

Thank you! It seems that I was trying to misuse callbacks in this case.

It’s not an unreasonable impulse, setting properties this way update date or plot elements, is just exactly what most callbacks do. This just happens to be one of a few areas where things are a bit more complicated (and why there are convenience APIs to help as described in the link).

1 Like