Here’s a minimum working example. It’s only a problem with the large image sizes (400x400), the normal player.py size of (10x10) doesn’t have the issue. So it must be the time lag due to image rendering. Any suggestions to make this smooth and responsive are appreciated.
-- coding: utf-8 --
“”"
An example of a simple player widget animating an Image demonstrating
how to connnect a simple HoloViews plot with custom widgets and
combine them into a bokeh layout.
The app can be served using:
bokeh serve --show player.py
“”"
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import Slider, Button
from bokeh.plotting import figure, ColumnDataSource
create image data
start = 0
end = 10
images = np.random.rand(400,400,end-start+1)
#create CDS and figure
simg = ColumnDataSource({‘images’:[images],‘image’:[images[…,0]],
‘x’:[0],‘y’:[0],‘dw’:[images.shape[1]],‘dh’:[images.shape[0]]})
p = figure(plot_width=512,plot_height=512,sizing_mode=‘scale_both’,
x_range=(0,images.shape[1]),y_range=(0,images.shape[0]))
p.image(source=simg,image=‘image’,x=‘x’,y=‘y’,dw=‘dw’,dh=‘dh’)
def animate_update():
year = slider.value + 1
if year > end:
year = start
slider.value = year
def slider_update(attrname, old, new):
print slider.value
tmp = simg.data[‘images’][0][…,slider.value]
print tmp.shape
simg.data[‘image’] = [tmp]
slider = Slider(start=start, end=end, value=0, step=1, title=“Year”)
slider.on_change(‘value’, slider_update)
def animate():
if button.label == ‘► Play’:
button.label = ‘❚❚ Pause’
curdoc().add_periodic_callback(animate_update, 200)
else:
button.label = ‘► Play’
curdoc().remove_periodic_callback(animate_update)
button = Button(label=‘► Play’, width=60)
button.on_click(animate)
layout = layout([
[p],
[slider, button],
], sizing_mode=‘fixed’)
curdoc().add_root(layout)
``
···
On Tuesday, August 29, 2017 at 3:05:48 PM UTC-4, [email protected] wrote:
I added a “movie” of frames of image data to my Bokeh app, following the Holoviews player.py example (http://holoviews.org/reference/apps/bokeh/player.html) (though I don’t use Holoview, just update ColumnDataSource of the p.image with the new frame).
When playing, it works fine. However, when I try to hit the “Pause”, the movie takes a long time to stop. Is there a way to force discarding of callbacks that have been triggered, but haven’t completed? Right now, when the Pause button is hit, it simply calls curdoc().remove_periodic_callback(animate_update)