Manufacturing Tap Events

I have a very complex callback that executes on a tap event and it works just fine.

What I’m trying to do is add a feature that allows the user to import a csv of what is essentially a list of xy locations that will essentially mimic the act of tapping in those places (which will subsequently trigger my very complex callback in the desired order).

So what I’m looking for/seeking advice about is how/if I can “manufacture” tap events given a list of xy locations to “artificially” tap?

Just wondering if anyone has any thoughts/ideas on how to accomplish this before I dive deep into my existing callback to essentially write a separate version to handle this feature :-/

Edit - Guess i’ll add that the csv import part is all taken care of too.

Oh boy this some crazy stuff, but I think I figured it out. I read about the base Event class for bokeh.events and saw that you can kinda initialize a particular event type via json/dictionary. Then I noticed that on the JS side you can call .trigger_event on a figure object. With that in hand I messed around with what the “dictionary” input should be, and here it is.

Idea of this example is to trigger a tap event on a figure at a given location by clicking a button.

from bokeh.plotting import figure, show, save
from bokeh.events import Tap
from bokeh.models import CustomJS, Button
from bokeh.layouts import column

fig = figure(x_range=[0,30],y_range=[0,30])
r = fig.line(x=[0,20],y=[0,30])

#seems like 'event_name' and "tap" are recognized, the key-values are arbitrary?
d = {"event_name": "tap", 'x':10,'y':20}

#this callback to happen when the figure "feels" a tap event
cb_tap = CustomJS(args=dict(src= r.data_source)
              ,code='''
              console.log('TAP HAPPENING')
              console.log(cb_obj)
              console.log(cb_obj.x)
              console.log(cb_obj.y)
              src.data['x'] = [0,cb_obj.x]
              src.data['y'] = [0,cb_obj.y]
              src.change.emit()
              ''')          
fig.js_on_event(Tap,cb_tap)

#this callback to be triggered when you click the button, and all it will do is try to trigger a tap event
cb_simtap = CustomJS(args=dict(fig=fig,d=d)
                     ,code='''
                     fig.trigger_event(d)
                     ''')
btn = Button(label='simtap')
btn.js_on_click(cb_simtap)
save(column([fig,btn]),'dummy.html') 

insane

As always any elaboration on the mechanics of how this works much appreciated.

2 Likes

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