6
我正在尝试一个示例Bokeh Application(以“单一模块格式”)从数据集生成图表。在给定的例子中,网页上的用户可以点击一个按钮,图表将会更新最新的数据。我想弄清楚如何在不需要用户点击按钮的情况下实现相同的行为。也就是说,我希望图表以指定的时间间隔自动更新/刷新/重新加载,而无需用户交互。理想情况下,我只需改变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))