I am trying to get the mouse coordinates when a user taps on a given plot. I am using bokeh.client and pushing curdoc() and also using session.loop_until_closed()
I can get the code to run when I used bokeh serve --show events.py, but when I start a bokeh server and execute the script in an IDE, I am unable to print the coordinates.
The code I have so far is shown below:
import numpy as np
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh import events
from bokeh.models import CustomJS, Div, Button
from bokeh.layouts import column, row
from bokeh.client import push_session
def print_event(attributes=[]):
"""
Function that returns a Python callback to pretty print the events.
"""
def python_callback(event):
for attr in attributes:
print(event.__dict__[attr])
return python_callback
# Follows the color_scatter gallery example
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
"#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)
]
p = figure(tools="pan,wheel_zoom,zoom_in,zoom_out,reset")
p.scatter(x, y, radius=radii,
fill_color=colors, fill_alpha=0.6,
line_color=None)
layout = column(p)
point_attributes = ['x', 'y']
# Point events
p.on_event(events.Tap, print_event(attributes=point_attributes))
p.on_event(events.DoubleTap, print_event(attributes=point_attributes))
curdoc().add_root(layout)
session = push_session(curdoc())
session.show()
session.loop_until_closed()
I am trying to get the mouse coordinates when a user taps on a given plot. I am using bokeh.client and pushing curdoc() and also using session.loop_until_closed()
I can get the code to run when I used bokeh serve --show events.py, but when I start a bokeh server and execute the script in an IDE, I am unable to print the coordinates.
The code I have so far is shown below:
import numpy as np
from bokeh.io import curdoc
from bokeh.plotting import figure
from bokeh import events
from bokeh.models import CustomJS, Div, Button
from bokeh.layouts import column, row
from bokeh.client import push_session
def print_event(attributes=):
"""
Function that returns a Python callback to pretty print the events.
"""
def python_callback(event):
for attr in attributes:
print(event.__dict__[attr])
return python_callback
# Follows the color_scatter gallery example
N = 4000
x = np.random.random(size=N) * 100
y = np.random.random(size=N) * 100
radii = np.random.random(size=N) * 1.5
colors = [
"#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)
]
p = figure(tools="pan,wheel_zoom,zoom_in,zoom_out,reset")
p.scatter(x, y, radius=radii,
fill_color=colors, fill_alpha=0.6,
line_color=None)