Is it possible to have both an internal update()
function and a js
callback attached to a slider in Bokeh.
For example, this code below is one of the Bokeh examples:
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.plotting import Figure, output_file, show
output_file("js_on_change.html")
x = [x*0.005 for x in range(0, 200)]
y = x
source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_width=400, plot_height=400)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
callback = CustomJS(args=dict(source=source), code="""
var data = source.data;
var f = cb_obj.value
var x = data['x']
var y = data['y']
for (var i = 0; i < x.length; i++) {
y[i] = Math.pow(x[i], f)
}
source.change.emit();
""")
slider = Slider(start=0.1, end=4, value=1, step=.1, title="power")
slider.js_on_change('value', callback)
layout = column(slider, plot)
show(layout)
What I would like to do is have an update function:
def update():
# Read a new bit of data.
# Some pandas stuff.
# source.data = new_data
So I can have:
# This will run a JS based callback.
slider.js_on_change('value', callback)
# This will run an internal function to fetch new piece of data.
slider.on_change('value', lambda attr, old, new: update())
So I can both update my underlying data and perform a JS event. Is this possible?
--- EDIT ---
Just adding a GIF file to demonstrate the problem. You can see the JS callback constantly fitting and updating the y_scale
of the plot to make all candles fit the plot.
However, the moment the pull-down menu is switched to another value, the y_scale
callback is not triggered. Only after we start to drag, the js
callback kicks in and adapts the y_scale
.