I am having some troubles while trying to update my Bokeh source when using a Select widget and CustomJS. CustomJS is a mandatory requirement in this case, so no def: function can be used. The below code returns the data visualisation but it does not refresh as, I guess, the JS code is not properly written and I got no way to debug it (if someone knows how, please its indeed the best answer I could get).
The main idea is to filter my source based on the value selected on the Select widget.
Any ideas??
source = ColumnDataSource(data=dict(
x=gx,
y=gy,
recty=recty,
colors=colors,
filter_info= filter_info))
select = Select(title="Option:", value="All", options=["All", "1", "2", "3", "4", "5", "6+"])
renderer_source = source
# Add the slider
code = """
var value_filter_info = cb_obj.value;
if value_assignments == 'All' {
new_source_data['x'] = source.data['x'];
new_source_data['y'] = source.data['y'];
new_source_data['recty'] = source.data['recty'];
new_source_data['colors'] = source.data['colors'];
} else {
new_source_data['x'] = source.data['x'][source.data['filter_info']==value_filter_info];
new_source_data['y'] = source.data['y'][source.data['filter_info']==value_filter_info];
new_source_data['recty'] = source.data['recty'][source.data['filter_info']==value_filter_info];
new_source_data['colors'] = source.data['colors'][source.data['filter_info']==value_filter_info];
}
renderer_source.data= new_source_data;
renderer_source.change.emit();
"""
callback = CustomJS(args=dict(source=source, renderer_source=renderer_source), code=code)
select.js_on_change('value', callback)
p = figure(plot_width=1200, plot_height=400, x_range=(0,100), y_range=(0,1000))
p.rect(x='x', y='recty', width=1, height=1, color='colors', source=renderer_source, line_color=None, fill_alpha=1, width_units="data", height_units="data")
plot_layout = column(select,p)
show(plot_layout)
output_notebook()
Many thanks!
So I found a solution that can be aesthetically, programatically, and visually improved but at least the outcome is shown.
Following the advice by @bigreddot on indexing and overwriting, I managed to write the current CustomJS code:
code = """
var render=renderer_source.data;
var data=source.data;
var data_assignment = data['assignments'];
var value_assignments = cb_obj.value;
var data_x=data['x'];
var data_y=data['y'];
var data_recty=data['recty'];
var data_colors=data['colors'];
var render_x=render['x'];
var render_y=render['y'];
var render_recty=render['recty'];
var render_colors=render['colors'];
var x = [];
var y = [];
var recty = [];
var colors = [];
render_x=[];
render_y=[];
render_recty=[];
render_colors=[];
for (var i=0;i<data_assignment.length; i++){
if (value_assignments == 'All') {
x.push(data_x[i]);
y.push(data_y[i]);
recty.push(data_recty[i]);
colors.push(data_colors[i]);
} else if (data_assignment[i]==value_assignments) {
x.push(data_x[i]);
y.push(data_y[i]);
recty.push(data_recty[i]);
colors.push(data_colors[i]);
}
}
renderer_source.data['x']=x;
renderer_source.data['y']=y;
renderer_source.data['recty']=recty;
renderer_source.data['colors']=colors;
renderer_source.change.emit();
"""