Bokeh - get information about selected points

I have a few things that I put into a scatter plot and display in a web browser window (using Bokeh).

I use PolySelectTool or BoxSelectTool for selection.

There are two things I would like to do: 1) Get information about the points that were selected in order to calculate some additional information. 2) Since dots represent URLs, I would like the chart to open a new browser tab and load a specific URL whenever I click a dot (representing a URL).

I don't think the code is important. But to finish my question, here it is ...

Y = my_data
urls = get_urls(my_data)

TOOLS="pan,wheel_zoom,reset,hover,poly_select,box_select"
p = figure(title = "My chart", tools=TOOLS)
p.xaxis.axis_label = 'X'
p.yaxis.axis_label = 'Y'

source = ColumnDataSource(
    data=dict(
        xvals=list(Y[:,0]),
        yvals=list(Y[:,1]),
        url=urls
    )
)
p.scatter("xvals", "yvals",source=source,fill_alpha=0.2, size=5)
hover = p.select(dict(type=HoverTool))
hover.snap_to_data = False
hover.tooltips = OrderedDict([
    ("(x,y)", "($x, $y)"),
    ("url", "@url"),
])

select_tool = p.select(dict(type=BoxSelectTool))

# 
# I guess perhaps something should be done with select_tool
#

show(p)

      

+3


source to share


1 answer


You can get information using a property source.selected

, if you want to be notified of all changes, you must create a callback, it would be something like this:

def callback(obj, attr, old, new):
    ...

source.on_change('selected', callback)

      



See this example for details .

+4


source







All Articles