如何为散景中的动画实现启动/停止/播放按钮?

时间:2019-03-31 11:14:39

标签: python-3.x animation widget bokeh

我正在尝试为散景动画实现星号停止按钮。我正在将bokeh服务器与curdoc()函数结合使用,但是到目前为止,我还没有取得太大的成功。

我想知道有更多经验的人会怎么做吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您在这里。使用bokeh serve --show app.py运行(已在Bokeh v1.0.4上测试)

from bokeh.models import ColumnDataSource, Toggle, Column
from bokeh.plotting import figure, curdoc
from datetime import datetime
import random

source = ColumnDataSource(dict(time = [datetime.now()], value = [random.randint(5, 10)]))
plot = figure(plot_width = 1200, x_axis_type = 'datetime', tools = 'pan,box_select,crosshair,reset,save,wheel_zoom')
plot.line(x = 'time', y = 'value', line_color = 'black', source = source)
toggle = Toggle(label = "Toggle", button_type = "success")

def update():
    if toggle.active:
        source.stream(dict(time = [datetime.now()], value = [random.randint(5, 10)]))

curdoc().add_root(Column(plot, toggle))
curdoc().add_periodic_callback(update, 1000)

结果:

enter image description here

相关问题