Bokeh:自动刷新散景图

2024-04-30

我正在尝试一个例子散景应用 http://bokeh.pydata.org/en/0.12.0/docs/user_guide/server.html#userguide-server-applications(以“单模块格式”)用于从数据集生成图表。在给定的示例中,网页上的用户可以单击按钮,图表将使用最新数据进行更新。我试图弄清楚如何在不要求用户单击按钮的情况下实现相同的行为。也就是说,我希望图表能够按照指定的时间间隔自动更新/刷新/重新加载,而不需要用户交互。理想情况下,我只需要更改 myapp.py 中的某些内容即可完成此操作。

散景版本是 0.12.0。

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

# 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 对象中有一个方法:

add_periodic_callback(回调,period_milliseconds) http://bokeh.pydata.org/en/0.12.0/docs/reference/document.html#bokeh.document.Document.add_periodic_callback

不知道为什么 API 之外没有提到这一点......

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Bokeh:自动刷新散景图 的相关文章

随机推荐