Hi all,
I’m trying to add annotations from a server callback. The callback should “wrap” a tapped point with a region on its sides.
My code looks like the following:
import numpy as np
from bokeh.models import BoxAnnotation
from bokeh.server.server import Server
from bokeh.plotting import figure
def _app(doc):
p = figure(tools=['tap'])
x = np.random.random(size=(1, 1000)) - .5
x_cum = np.cumsum(x, axis=1)
t = np.arange(len(x))
s = p.scatter(t, x_cum)
def handler(attr, old, new):
print('attr: {} old: {} new: {}'.format(attr, old, new))
new_layout = []
for t in new:
new_layout.append(BoxAnnotation(left=t - 5, right=t + 5, fill_color='green', fill_alpha=.3, level='underlay'))
p.center = new_layout
s.data_source.selected.on_change('indices', handler)
doc.add_root(p)
server = Server({'/': _app}, num_procs=1)
server.start()
if __name__ == '__main__':
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()
I also tried p.add_layout
instead of setting p.center
but nothing. I saw that for Jupyter notebooks there’s the push_notebook()
function that might’ve worked here, so maybe something similar for a server will work?
What am I doing wrong?