TapTool help/documentation/examples

Hi,

First I wanted to say great job! I’ve been desperately looking for a python alternative to d3.js and was very happy to discover bokeh which seems to fit my requirements perfectly.

However, I couldn’t find any documentation/examples explaining how to use the TapTool (server side, no JS!). Specifically, how can I get a reference to the clicked object?

Thank you, Yuri.

Hi Yuri,

Tap tool is responsible for selecting a portion of the source. So, in this case you can track the source and trap the changes on the source selected attribute. Here’es a modified version of the server/tap.py example that should show you what you need:

The plot server must be running

Go to http://localhost:5006/bokeh to view this plot

from future import division

import itertools

import numpy as np

from six.moves import zip

from bokeh.plotting import *

from bokeh.models import TapTool

xx, yy = np.meshgrid(range(0,101,4), range(0,101,4))

x = xx.flatten()

y = yy.flatten()

N = len(x)

inds = [str(i) for i in np.arange(N)]

radii = np.random.random(size=N)*0.4 + 1.7

colors = [

“#%02x%02x%02x” % (r, g, 150) for r, g in zip(np.floor(50+2x), np.floor(30+2y))

]

foo = list(itertools.permutations(“abcdef”))[:N]

bar = np.random.normal(size=N)

source = ColumnDataSource(

data=dict(

x=x,

y=y,

radius=radii,

colors=colors,

foo=foo,

bar=bar,

)

)

output_server(“tap”)

TOOLS=“crosshair,pan,wheel_zoom,box_zoom,reset,tap,previewsave”

p = figure(title=“Tappy Scatter”, tools=TOOLS)

p.circle(x, y, radius=radii, source=source,

fill_color=colors, fill_alpha=0.6,

line_color=None, name=“mystuff”)

p.text(x, y, text=inds, alpha=0.5, text_font_size=“5pt”,

text_baseline=“middle”, text_align=“center”)

in the broswer console, you will see messages when circles are clicked

tool = p.select(dict(type=TapTool))[0]

tool.names.append(“mystuff”)

def on_selection_change(obj, attr, old, new):

print “HIT!”, old, new

renderer = p.select(dict(name=“mystuff”))

scatter_ds = renderer[0].data_source

scatter_ds.on_change(‘selected’, on_selection_change)

show§ # open a browser

cursession().poll_document(curdoc(), 0.05)

``

Thanks!

Fabio

···

On Tuesday, April 28, 2015 at 7:01:36 PM UTC+2, [email protected] wrote:

Hi,

First I wanted to say great job! I’ve been desperately looking for a python alternative to d3.js and was very happy to discover bokeh which seems to fit my requirements perfectly.

However, I couldn’t find any documentation/examples explaining how to use the TapTool (server side, no JS!). Specifically, how can I get a reference to the clicked object?

Thank you, Yuri.