Skip to content

Instantly share code, notes, and snippets.

@synapticarbors
Created July 8, 2018 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save synapticarbors/a701f4fb202764a329e2441e1e92b8df to your computer and use it in GitHub Desktop.
Save synapticarbors/a701f4fb202764a329e2441e1e92b8df to your computer and use it in GitHub Desktop.
Bokeh issue
import numpy as np
from bokeh.layouts import column, row
from bokeh.models import CustomJS, ColumnDataSource, Slider
from bokeh.plotting import figure, output_file, show
from bokeh.core.property.containers import PropertyValueDict
output_file('test.html')
default_y0 = 0.0
default_y1 = 10.0
N = 10
x = np.linspace(0, 10, N)
y0_arr = default_y0 * np.ones(N)
y1_arr = default_y1 * np.ones(N)
source = ColumnDataSource(data=dict(
x=x,
y0=y0_arr,
y1=y1_arr,
))
state = PropertyValueDict(dict(
y0=default_y0,
y1=default_y1))
update_plots = """\
var data = source.data;
var y0_val = state['y0'];
var y1_val = state['y1'];
var x = data['x'];
var y0 = data['y0'];
var y1 = data['y1'];
for (var i = 0; i < x.length; i++) {
y0[i] = y0_val
y1[i] = y1_val
}
source.change.emit();
"""
def make_change_value_callback(element):
return CustomJS(args=dict(state=state, source=source), code="""
state['{}'] = cb_obj.value;
""".format(element) + update_plots)
y0_slider_callback = make_change_value_callback('y0')
y1_slider_callback = make_change_value_callback('y1')
y0_slider = Slider(start=1, end=100, value=default_y0, step=0.1, title="y0")
y0_slider.js_on_change('value', y0_slider_callback)
y1_slider = Slider(start=1, end=100, value=default_y1, step=0.1, title="y1")
y1_slider.js_on_change('value', y1_slider_callback)
s1 = figure(plot_width=500, plot_height=400, title='test')
s1.line(x='x', y='y0', source=source, line_color='blue', legend='y0')
s1.line(x='x', y='y1', source=source, line_color='red', legend='y1')
show(column(
row(y0_slider, y1_slider),
s1
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment