Python Bokeh将播放按钮添加到滑块 [英] Python Bokeh add a play button to a slider

查看:59
本文介绍了Python Bokeh将播放按钮添加到滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的可视化需要一个播放按钮来自动浏览滑块上的值,并在到达末尾时循环播放.有没有办法做到这一点?谢谢.

my following visualization needs a play button to automatically go through the values on the slider and loop when reaches the end. Is there a way to achieve that? Thanks.

精确的结果类似于此链接上的图: https://demo.bokeh.org/gapminder

The expacted outcome is like the graph on this link: https://demo.bokeh.org/gapminder

# Bokeh Visualisation (0.12.7)

from bokeh.charts import Scatter, output_file, show
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.io import output_file, show
from bokeh.layouts import layout, widgetbox, column, row
from bokeh.models import Toggle, BoxAnnotation, NumeralTickFormatter, CategoricalColorMapper
from bokeh.models import HoverTool
from bokeh.models.widgets import RadioButtonGroup, Slider
from bokeh.models.callbacks import CustomJS

dataset = {'x':[0,1,2],'y':[0,1,2], 'x_filter':[0,1,2]}

source2 = ColumnDataSource(data=dataset)


p2 = figure(plot_width=600, plot_height=600, 
            x_range=(-5,50), y_range=(-5,10))

p2.scatter('x', 'y', source=source2,
          size=15,
          alpha=0.8, 
          line_color=None)

p2.title.text = 'old title'

# add callback to control 
callback = CustomJS(args=dict(p2=p2, source2=source2), code="""

            var slider_value = cb_obj.value;
            var data = source2.data;            
            x = data['x']
            x_filter = data['x_filter']
            y = data['y']

            for (i = 0; i < x.length; i++) {
                x[i] = x_filter[i]+slider_value
            }

            p2.title.text = 'new title'

        source2.change.emit();

        """)

slider = Slider(start=0, end=50, value=0, step=5, title="test", callback=callback)


show(column(widgetbox(slider),p2))

推荐答案

您可以在bokeh的github上找到所有应用程序的代码,这是gapminder https://github.com/bokeh/bokeh/blob/master/examples/app/gapminder/main.py

You can find the codes of all the apps on the github of bokeh, here is the gapminder https://github.com/bokeh/bokeh/blob/master/examples/app/gapminder/main.py

您正在寻找的(正在使用bokeh服务器):

you are looking for that (that's using a bokeh server):

def animate_update():
    year = slider.value + 1
    if year > years[-1]:
        year = years[0]
    slider.value = year


def slider_update(attrname, old, new):
    year = slider.value
    label.text = str(year)
    source.data = data[year]

slider = Slider(start=years[0], end=years[-1], value=years[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)

这篇关于Python Bokeh将播放按钮添加到滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆