Hi,
I am having trouble connecting my slider object through a callback. I am trying to change the data on the plot based on the slider value. I am using bokeh 1.3.4 but can use the latest version (2.2 I believe) if needed, but I found it easier to use 1.3.4.
I am using the output of a tsne model to plot data through a scatter plot on Bokeh. Everything works fine except the callback for the slider.
Slider Code:
y_labels = y_pred
#data sources
#desc contains the cluster value for each record
source = ColumnDataSource(data=dict(
x=vis_x, y=vis_y, x_backup=vis_x, y_backup=vis_y, desc=y_labels, message=df['message'], date=df['date'], forms=df['forms'], status=df['status'], substatus=df['substatus'], response=df['response'], labels=["C-" + str(x) for x in y_labels] ))
# map colors
mapper = linear_cmap(field_name='desc',
palette=Category20[10],
low=min(y_labels), high=max(y_labels))
# prepare the figure
plot = figure(plot_width=1200, plot_height=850,
tools=['lasso_select', 'box_select', 'pan', 'box_zoom', 'reset', 'save', 'tap'],
title="Clustering and LDA",
toolbar_location="above")
# plot settings
plot.scatter('x', 'y', size=5,
source=source,
fill_color=mapper,
line_alpha=0.3,
line_color="black",
legend='labels')
plot.legend.background_fill_alpha = 0.6
# Keywords
text_banner = Paragraph(text='Keywords: Slide to specific cluster to see the keywords.', height=45)
input_callback_1 = input_callback(plot, source, text_banner, topics)
# currently selected
div_curr = Div(text="""Click on a plot to see more information.""", height=150)
callback_selected = CustomJS(args=dict(source=source, current_selection=div_curr),
code=selected_code())
taptool = plot.select(type=TapTool)
taptool.callback = callback_selected
# WIDGETS - HERE IS WHERE THE PROBLEM BEGINS
slider = Slider(start=0, end=10, value=10, step=1, title="Cluster #", callback=input_callback_1)
# pass call back arguments input_callback_1.args["slider"] = slider
āāā
Callback Code:
def input_callback(plot, source, out_text, topics):
# slider call back for cluster selection
callback = CustomJS(args=dict(p=plot, source=source, out_text=out_text, topics=topics), code="""
var data = source.data;
var cluster = slider.value;
x = data['x'];
y = data['y'];
x_backup = data['x_backup'];
y_backup = data['y_backup'];
labels = list(set(data['desc']));
desc = data['desc']
message = data['message'];
date = data['date'];
forms = data['forms'];
status = data['status'];
substatus = data['substatus'];
hac_code = data['hac_code'];
if (cluster == '10') {
out_text.text = 'Keywords: Slide to specific cluster to see the keywords.';
for (i = 0; i < x.length; i++) {
if(desc[i] == cluster) {
x[i] = x_backup[i];
y[i] = y_backup[i];
} else {
x[i] = undefined;
y[i] = undefined;
}
}
}
else {
out_text.text = 'Keywords: ' + console.log(topics[Number(cluster)].toString());
for (i = 0; i < x.length; i++) {
if(desc[i] == cluster) {
x[i] = x_backup[i];
y[i] = y_backup[i];
} else {
x[i] = undefined;
y[i] = undefined;
}
}
}
source.change.emit();
""")
return callback
āāā
I have been trying to understand callbacks and CustomJS for a week and Iām not getting anywhere. any help is appreciated!