Adding a point to a bokeh plot on click

Viewed 6090

I'm trying to set up a bokeh plot where the user can click on the plot to add a point. I've seen this example which uses the BoxSelectTool to add Rect glyphs to the plot, however I'm looking for a way to add circle glyphs centered at the click location. I would also like to then send these points back to the server side of things. Anyone have any experience doing something similar?

1 Answers

This works for me in Bokeh version 0.12.13:

from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Column
from bokeh.io import curdoc
from bokeh.events import DoubleTap

coordList=[]

TOOLS = "tap"
bound = 10
p = figure(title='Double click to leave a dot.',
           tools=TOOLS,width=700,height=700,
           x_range=(-bound, bound), y_range=(-bound, bound))

source = ColumnDataSource(data=dict(x=[], y=[]))   
p.circle(source=source,x='x',y='y') 

#add a dot where the click happened
def callback(event):
    Coords=(event.x,event.y)
    coordList.append(Coords) 
    source.data = dict(x=[i[0] for i in coordList], y=[i[1] for i in coordList])        
p.on_event(DoubleTap, callback)

layout=Column(p)

curdoc().add_root(layout)

(to run it save this script as something.py and run in a cmd: bokeh serve something.py --show )

enter image description here

Related