
Is there a proper way to remove a callback from a document? I tried to follow the structure of the old Gapminder example but haven’t been able to stop the animation when it ends.
webinstance = curdoc()
index = 1
callback_id = None
def modify_doc(doc):
N=100
X = np.linspace(-2*np.pi, 2*np.pi, N)
Y = 0.5 * np.sin(3 * X)
p = figure(plot_height=500, plot_width=1000, title="Test", x_range=(-2*np.pi,2*np.pi), y_range=(-0.6,0.6))
ypts = ColumnDataSource(data=dict(x=[X[0]], y=[Y[0]]))
p.circle(x="x", y="y", size=9, source=ypts)
def update():
global index
global callback_id
new_data = dict(x=[X[index]], y=[Y[index]])
ypts.stream(new_data)
print(index)
index += 1
if index < N-1:
callback_id = doc.add_periodic_callback(update, 40)
else:
doc.remove_periodic_callback(callback_id)
doc.add_root(p)
doc.title = "Test"
modify_doc(webinstance)

