首页 > 解决方案 > 散景服务器:无法将回调附加到字形点击,例如圆圈?

问题描述

单击字形时,我一直在尝试调用 python 函数。我试过这个例子https://stackoverflow.com/a/50499396/8925535和这个https://groups.google.com/a/continuum.io/g/bokeh/c/zVzLyvJFWtE/m/eRugLfUuAAAJ但仍然我的回调永远不会被调用。这是我的代码

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) 

有问题的回调函数是tap_callback。单击字形后,我真的很想在终端中看到“Here mate”

标签: pythonbokeh

解决方案


代替

source.on_change('selected', tap_callback)

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

推荐阅读