Get time from time slider

Hi,
The UI has a time slider and a play button. I am trying to take the specific time when user click pause but I am not sure how to retrieve the value outside the animate() function.
Really appreciate your time and help!

import math
from bokeh.io import save, curdoc
from bokeh.layouts import column, row, gridplot
from bokeh.model import Model
from bokeh.models import CustomJS, Slider, Callback, HoverTool, Button
from bokeh.plotting import ColumnDataSource, figure, show
from bokeh.models.widgets import Panel, Tabs
import numpy as np
import pandas as pd

t0, tf = 0.0, 43200.0 # 12hrs
N = 25
tspan = np.linspace(t0, tf, N)
time_step = tspan[1] 
slider_time = Slider(title="Time Slider (s)", value=t0, start=t0, end=tf, step=time_step, width=300)

def animate_update():
    current_time = slider_time.value + time_step
    if current_time > tf:
        current_time = t0
    slider_time.value = current_time

def animate():
    global callback_id
    if animate_button.label == '► Play':
        animate_button.label = '❚❚ Pause'
        callback_id = curdoc().add_periodic_callback(animate_update, 1*450.0) # s to milliseconds conversion
    else:
        animate_button.label = '► Play'
        curdoc().remove_periodic_callback(callback_id)

animate_button = Button(label='► Play', width=80)
animate_button.on_event('button_click', animate)

layout = (column(slider_time, animate_button))
curdoc().add_root(layout)

I am not sure how to retrieve the value outside the animate() function.

What does this mean exactly? In general you can only access Bokeh models and their properties in other callbacks. Is there another callback you are planning to add that is not shown here?

In any case, think that will be tricky with the current approach of adding/removing callbacks. That is because remove_periodic_callback schedules something to happen in the near future, and I don’t think it can be guaranteed that the slider update won’t happen one more time before the scheduled removal actually happens. In which case recording in the slider value in animate might be off by one slider update.

I think instead you could leave the animate callback always running, and then instead of a global callack_id value, have a global is_playing boolean value that the button callback sets. Then animate moves the slider, if that value is set to true. Then you can be sure, in the button callback, that the slider will not advance any more after it sets is_playing to False.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.