Is it possible to use js_on_change
with bokeh
to dynamically update text that is placed next to a plot?
For example, using this code snippet from a different question
from random import random
from bokeh.models import CustomJS, ColumnDataSource, Span
from bokeh.plotting import figure, output_file, show
output_file("callback.html")
x = [random() for x in range(500)]
y = [random() for y in range(500)]
color = ["navy"] * len(x)
s = ColumnDataSource(data=dict(x=x, y=y, color=color))
p = figure(plot_width=400,
plot_height=400,
tools="lasso_select",
title="Select Here")
p.circle(x='x', y='y', color='color', size=8, source=s, alpha=0.4)
slope = Span(location=.5,
dimension="width",
line_alpha=.6,
line_width=5)
p.add_layout(slope)
s.selected.js_on_change(
'indices',
CustomJS(args=dict(s=s, slope=slope),
code="""
var inds = cb_obj.indices;
if (inds.length == 0) {
slope.location = 0.5
return
}
var total = 0;
for (var i = 0; i < inds.length; i++) {
total += s.data["y"][inds[i]]
}
var avg = total / inds.length;
slope.location = avg;
"""))
show(p)
I'd like to include a text right of the figure that shows the value of the computed slope.location
and updates whenever I select new points.