I am trying to get the updated slider values after the user moves the slider, but nothing I tried seems to work. Looking at other posts I tried ```slider.on_change(‘value’, update)`` but got a warning that " you can’t have js_on_change and on_change." Using the Bokeh example code
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, Slider
from bokeh.plotting import figure, output_file, show
output_file("callback.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)
If the user moves the slider to 1.90, how do I get that slider value?
@p-himik The value that returns with cb_obj.value is the starting value not the updated value after the slider is moved. I am trying to use the slider as an input for further data analysis, but that only works if after the slider is moved I can get the updated value.
In 2.2.3 (and in any other version, AFAIK) it is absolutely the new value and not the old one. Here’s a screen recording after I added console.log(f) to the callback code. I’m moving the focused slider by pressing the left and right arrow keys.
@tpike3 are you asking about how to get the updated value of slider.value in Python code, in a notebook? You will have to embed a Bokeh server app for that, otherwise show is just one-way to create standalone HTML+JS that is not connected to any python process. Here is an example notebook for embedding Bokeh server,
Otherwise on OSX your code works as expected outside the notebook with no changes, and inside the notebook with replacing output_file with output_notebook, You’ll need to be more specific about your platform and versions.
@Bryan and @p-himik Thanks for all your help, I learned a ton about JS and Bokeh. Looking at the documentation @Bryan, I learned what I really wanted was Jupyter Interactors. I refactored my code and now the slider values serve as input to the next section.