1

I am trying to get a callback function to work in bokeh based on this simple example:

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = "tap,reset"
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(x=[[1, 3, 2], [3, 4, 6, 6]],
                y=[[2, 1, 4], [4, 7, 8, 5]], name=['A', 'B']))

pglyph = p.patches('x', 'y', source=source)

def callback(attr, old, new):
    # The index of the selected glyph is : new['1d']['indices'][0]
    print("In callback")
    patch_name =  source.data['name'][new['1d']['indices'][0]]
    print("TapTool callback executed on Patch {}".format(patch_name))

pglyph.data_source.on_change('selected',callback)

curdoc().add_root(column(p))

When I load the page and click on a polygon, I do not see the callback getting executed.

What is missing?

1 Answer 1

4

That's because the selected attribute value is not changed. The contained object is changed instead, and Bokeh doesn't detect deep changes.

Try replacing the callback function and the next line with:

def callback(attr, old, new):
    print("In callback")
    patch_name = source.data['name'][new[0]]
    print("TapTool callback executed on Patch {}".format(patch_name))


pglyph.data_source.selected.on_change('indices', callback)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.