
I am trying to change the visualized data by using a slider. Interestingly, on slider change, the console logs the expected values but the visualization is not updated as expected. I put the data into 2d arrays then use the slider value to index into the array. The working code is below.
Why is the code logging the right data but the graph is not being updated?
from bokeh.models import CustomJS, Slider
from bokeh.plotting import ColumnDataSource, figure, output_file, show
from bokeh.embed import file_html
from bokeh.resources import CDN
import pandas as pd
df_str = '''2020-09-10 1 75731 0
2020-09-10 2 71627 0
2020-09-10 3 66972 0
2020-09-10 4 63718 0
2020-09-10 5 61951 0
2020-09-11 1 59380 1
2020-09-11 2 58297 1
2020-09-11 3 57833 1
2020-09-11 4 55669 1
2020-09-11 5 55281 1'''
df = pd.DataFrame([col.split() for col in [row for row in df_str.split('\n')]])
df.columns = ['Date', 'Score', 'ScoreTotal', 'DayNumber']
dates = df.Date.unique()
xs = [[] for _ in range(len(dates))]
ys = [[] for _ in range(len(dates))]
date = [i for i in range(len(dates))]
for _, row in df.iterrows():
i = int(row.DayNumber)
xi = row.Score
yi = row.ScoreTotal
xs[i].append(xi)
ys[i].append(yi)
x = xs[0]
y = ys[0]
xs += [[]] * 3
ys += [[]] * 3
source = ColumnDataSource(data=dict(x=x, y=y, xs=xs, ys=ys))
plot = figure(plot_width=500, plot_height=500)
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6)
date_slider = Slider(start=0, end=2, value=0, step=1, title="Date Slider")
callback = CustomJS(args=dict(source=source, date_slider=date_slider),
code="""
const data = source.data;
const date = date_slider.value
const x = data['xs'][date]
const y = data['ys'][date]
console.log(y) // CORRECT CONSOLE LOG BUT NO CHANGE
source.change.emit();
""")
date_slider.js_on_change('value', callback)
from bokeh.layouts import column, row
layout = row(
plot,
column(date_slider),
)
show(layout)

