How can I define an event on the server that captures the $index value of the tooltip with a tap or double tap on the quad?

I’m trying to define an event in the bojeh server that through a callback function allows you to locate in a glyph the value $index that I read in the tooltip with a tap or double tap. I attach an example.

from bokeh.io import curdoc,show
from bokeh.models import ColumnDataSource
from bokeh.plotting import Figure
from bokeh.layouts import row
from bokeh.events import Tap
from bokeh.events import DoubleTap
from bokeh.models import TapTool
from functools import partial
def callbackTapGantt(sorgente,nomeColonna,figura):
print(sorgente)
print(nomeColonna)
print(sorgente.data[nomeColonna])
selected = sorgente.selected.indices
print(“selected=”,sorgente.selected)
print(“selected=”,sorgente.selected.indices)
print(“the $index value ???”)
return
CDS = ColumnDataSource(
data = dict(Color = [‘green’,‘blue’,‘red’],
Start_dt = [1,2,3],
End_dt = [3,4,5],
L = [0.8,1.8,2.8],
H= [1.2,2.2,3.2],
listaTag=[‘one’,‘two’,‘zero’])
)
listaTag=[‘one’,‘two’,‘zero’]
TOOLTIPS = [
(“index”, “$index”),
]
fig = Figure(plot_width=300, plot_height=300,name=“figure1”,tooltips=TOOLTIPS)
q1=fig.quad(left=‘Start_dt’, right=‘End_dt’, bottom=‘L’, top=‘H’,source=CDS,
color=‘Color’,tags=listaTag,name=‘quad1’)
fig.on_event(Tap,partial(callbackTapGantt,sorgente=CDS,nomeColonna=‘listaTag’,figura=fig))
layout = row(fig)
curdoc().add_root(layout)
Kind regards
Mauro

First of all, please format your code using the editor tools or ``` around the code.

You’re pretty close. I think you’re confusing maybe one or two things at most.

One:

  • the Tap event is used to record mouse clicks anywhere on a figure (good for getting x-y coords where the user clicks)
  • the TapTool is used to select a single or subset of a glyphs belonging to a specified renderer(s) (good for tgetting what they clicked on)

You want the latter here.

So, something like:

 # create a taptool that says "ok allow user to tap on this specific renderer" in your case, the quad renderer you created that's being driven by CDS (your columndatasource)
ttool=TapTool(renderers=[q1])
#add that tool to your figure
fig.add_tools(ttool)

Two:

  • You want to callback to trigger not when the user triggers a Tap event (i.e. NOT when they click ANYWHERE), but whenever the CDS.selected.indices property changes (because that means they’ve selected a new glyph)

So something like

def py_cb(attr,old,new):
    print(CDS.selected.indices)

CDS.selected.on_change('indices',py_cb)

Dear gmerritt123

Thanks YOU!!!

All works very fine!!!

Kind regards

Mauro

@mauroDia Friendly FYI in the future please use code formatting so that the code is intelligible (either with the </> icon on the editing toolbar, or triple backtick ``` fences around the code blocks)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.