I am trying to convert javascript callback example for Custom Range Update from bokeh callback tutorial to pyscript.
This is the working sample :-
import functools
import string
from bokeh.layouts import row
from bokeh.models import ColumnDataSource, CustomJS, Rect, String
from bokeh.plotting import figure, show
N = 4000
x = np.random.random(size=N)*100
y = np.random.random(size=N)*100
radii = np.random.random(size=N)*1.5
colors = [
"#%02x%02x%02x" % (int(r), int(g), 150) for r, g in zip(50+2*x, 30+2*y)
]
#x, y, radii, colors
source = ColumnDataSource({'x':[], 'y':[], 'width':[], 'height':[]})
p1 = figure(title='Pan and Zoom Here', x_range=(0, 100), y_range=(0, 100),
tools='box_zoom,wheel_zoom,reset', plot_width=400, plot_height=400)
p1.scatter(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)
p2 = figure(title='See Zoom Window here', x_range=(0, 100), y_range=(0, 100),
tools='', plot_width=400, plot_height=400)
rect = Rect(x='x', y='y', width='width', height='height', fill_alpha=0.1,
line_color='black', fill_color='black')
p2.scatter(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None)
p2.add_glyph(source, rect)
# PyScript functions
def callback_x(source=source, window=None):
data = source.get('data')
start = cb_obj.get('start')
end = cb_obj.get('end')
data['x'] = [start+(end-start)/2]
data['width'] = [end - start]
source.trigger('change')
def callback_y(source=source, window=None):
data = source.get('data')
start = cb_obj.get('start')
end = cb_obj.get('end')
data['y'] = [start+(end-start)/2]
data['height'] = [end - start]
source.trigger('change')
# End PyScript
p1.x_range.callback = CustomJS.from_py_func(callback_x)
p1.y_range.callback = CustomJS.from_py_func(callback_y)
layout = row(p1, p2)
show(layout)
I want to refactor callback_x, callback_y to something like :-
def callback(source=source,edge='x', edge_type='width', window=None):
data = source.get('data')
start = cb_obj.get('start')
end = cb_obj.get('end')
data[edge] = [start+(end-start)/2]
data[edge_type] = [end - start]
source.trigger('change')
How to write this code in terms of bokeh models?