sanjay
sanjay

Reputation: 717

Python callback in Bokeh issue

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?

Upvotes: 1

Views: 559

Answers (1)

Eugene Pakhomov
Eugene Pakhomov

Reputation: 10652

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)

Upvotes: 4

Related Questions