Search code examples
pythonbokeh

Bokeh Server: Trouble attaching callback to glyph tap e.g. a circle?


I've been trying to call a python function when a glyph is clicked. I've tried this example https://stackoverflow.com/a/50499396/8925535 and this https://groups.google.com/a/continuum.io/g/bokeh/c/zVzLyvJFWtE/m/eRugLfUuAAAJ but still my callback never gets called. Here's my code

from bokeh.models import Slider, ColumnDataSource
from bokeh.io import curdoc
from bokeh.layouts import row
from bokeh.plotting import figure
from bokeh.palettes import d3

from numpy.random import random

#Create data for the plot
initial_points = 5
data = {'x': random(initial_points),
        'y': random(initial_points)
       }

source = ColumnDataSource(data = {'x': random(initial_points), 'y': random(initial_points)})

plot = figure(title = "Random scatter plot generator", plot_height=750, plot_width=750, 
        tools="tap")
plot.circle(x = 'x', y = 'y', source = source,  color='green', size=20, alpha=0.5)

slider_widget = Slider(start = 0, end = 20, step = 2, value = initial_points, title = 'Slide 
     right to increase number of points')

def slider_callback(attr, old, new):
    points = slider_widget.value
    source.data = {'x': random(points), 'y': random(points)}
slider_widget.on_change('value', slider_callback)

def tap_callback(attr, old, new):
    print('Here mate')

source.on_change('selected', tap_callback)

layout = row(slider_widget, plot)
curdoc().add_root(layout) 

The callback function in question is tap_callback. I'd really love to see "Here mate" printed in terminal after clicking a glyph


Solution

  • Replace

    source.on_change('selected', tap_callback)
    

    with

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