散景:自动刷新散景图 [英] Bokeh: Automatically refreshing bokeh plots

查看:95
本文介绍了散景:自动刷新散景图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个示例

I'm trying out an example Bokeh Application (in 'single module format') for generating a chart from a dataset. In the given example, the user on the web page can click on a button and the chart will update with the latest data. I am trying to figure out how I can achieve this same behavior without requiring the user to click on the button. That is, I would like the chart to automatically update/refresh/reload at a specified interval without the need for user interaction. Ideally, I would only have to change something in myapp.py to accomplish this.

散景版本为0.12.0.

bokeh version is 0.12.0.

为方便起见,此处复制了演示代码:

Demo code copied here for convenience:

# myapp.py

import numpy as np

from bokeh.layouts import column
from bokeh.models import Button
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc

# create a plot and style its properties
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None

# add a text renderer to out plot (no data yet)
r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
            text_baseline="middle", text_align="center")

i = 0

ds = r.data_source

# create a callback that will add a number in a random location
def callback():
    global i
    ds.data['x'].append(np.random.random()*70 + 15)
    ds.data['y'].append(np.random.random()*70 + 15)
    ds.data['text_color'].append(RdYlBu3[i%3])
    ds.data['text'].append(str(i))
    ds.trigger('data', ds.data, ds.data)
    i = i + 1

# add a button widget and configure with the call back
button = Button(label="Press Me")
button.on_click(callback)

# put the button and plot in a layout and add to the document
curdoc().add_root(column(button, p))

推荐答案

结果证明Document对象中有一个方法:

Turns out there's a method in the Document object:

add_periodic_callback(回调,period_milliseconds)

不确定在API之外为何未提及此内容...

Not sure why this isn't mentioned outside of the API...

这篇关于散景:自动刷新散景图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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