python bokeh slider

Solutions on MaxInterview for python bokeh slider by the best coders in the world

showing results for - "python bokeh slider"
Daniel
04 Jul 2019
1import numpy as np
2
3from bokeh.layouts import column, row
4from bokeh.models import CustomJS, Slider
5from bokeh.plotting import ColumnDataSource, figure, output_file, show
6
7x = np.linspace(0, 10, 500)
8y = np.sin(x)
9
10source = ColumnDataSource(data=dict(x=x, y=y))
11
12plot = figure(y_range=(-10, 10), plot_width=400, plot_height=400)
13
14plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
15
16amp_slider = Slider(start=0.1, end=10, value=1, step=.1, title="Amplitude")
17freq_slider = Slider(start=0.1, end=10, value=1, step=.1, title="Frequency")
18phase_slider = Slider(start=0, end=6.4, value=0, step=.1, title="Phase")
19offset_slider = Slider(start=-5, end=5, value=0, step=.1, title="Offset")
20
21callback = CustomJS(args=dict(source=source, amp=amp_slider, freq=freq_slider, phase=phase_slider, offset=offset_slider),
22                    code="""
23    const data = source.data;
24    const A = amp.value;
25    const k = freq.value;
26    const phi = phase.value;
27    const B = offset.value;
28    const x = data['x']
29    const y = data['y']
30    for (var i = 0; i < x.length; i++) {
31        y[i] = B + A*Math.sin(k*x[i]+phi);
32    }
33    source.change.emit();
34""")
35
36amp_slider.js_on_change('value', callback)
37freq_slider.js_on_change('value', callback)
38phase_slider.js_on_change('value', callback)
39offset_slider.js_on_change('value', callback)
40
41layout = row(
42    plot,
43    column(amp_slider, freq_slider, phase_slider, offset_slider),
44)
45
46output_file("slider.html", title="slider.py example")
47
48show(layout)
49