Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bokeh - get information about points that have been selected

I have a several points that I plot into scatter plot and show in web browser window (using Bokeh).

For selection, I use PolySelectTool or BoxSelectTool.

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

I don't think the code is important. But to make my question complete, 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)
like image 338
Marek Avatar asked Jan 09 '15 08:01

Marek


1 Answers

You can get information with the source.selected property, if you want to be notified of every change 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 more details.

like image 92
elyase Avatar answered Sep 30 '22 00:09

elyase