无法使用散景流绘制线图

时间:2016-06-28 17:20:22

标签: python streaming bokeh

运行此代码时,我无法获得任何类型的输出(空白框)。

from bokeh.io import gridplot,vplot,hplot
from bokeh.models import ColumnDataSource, Slider, Select
from bokeh.plotting import curdoc, figure

source = ColumnDataSource(dict(time=[],virt=[]))

def update():
    with open('test.log', 'r') as f:
        f.seek(0, 2)
        cur = f.tell()
        f.seek((cur - 198))
        s = f.read(198)
        arr = s.replace('\n','').replace('[','').replace(']','').split(' ')
        new_data = dict(time=[arr[0]+" "+arr[1]], virt=[arr[11]])
        print(new_data)  # sample output {'virt': ['2912m'], 'time':['2016-06-28 13:09:57']}
        source.stream(new_data)


p2 = figure(tools="xpan,xwheel_zoom,xbox_zoom,reset", x_axis_type='datetime', y_axis_location="right")
p2.x_range.follow = "end"
p2.x_range.follow_interval = 100
p2.x_range.range_padding = 5

p2.line(x='time', y='virt', alpha=0.8, line_width=2, color='black', source=source)

curdoc().add_root(gridplot([[p2]],toolbar_location="left"))
curdoc().add_periodic_callback(update, 1000)
curdoc().title = "Server Logs"

请帮助我告诉我哪里出错?

1 个答案:

答案 0 :(得分:0)

通过将以下间隔添加到流回调,下面的代码使用散景版本0.11.1。

注意:如果没有f文件的副本,则必须在回调中随机生成流数据。

from bokeh.io import gridplot,vplot,hplot
from bokeh.models import ColumnDataSource, Slider, Select
from bokeh.plotting import curdoc, figure
import datetime   # temporary import
import random     # temporary import

source = ColumnDataSource(dict(time=[],virt=[]))

def update():
        new_data = dict(time=[datetime.datetime.now()], virt=[random.randint(1,11)*1000])
        print(new_data)  # sample output {'virt': ['2912m'], 'time':['2016-06-28 13:09:57']}
        source.stream(new_data, 100) # follow interval supplied to the stream


p2 = figure(tools="xpan,xwheel_zoom,xbox_zoom,reset", x_axis_type="datetime", y_axis_location="right")

#p2.x_range.follow = "end"
#p2.x_range.follow_interval = 100
#p2.x_range.range_padding = 5

p2.line(x='time', y='virt', alpha=0.8, line_width=2, color='black', source=source)

curdoc().add_root(gridplot([[p2]],toolbar_location="left"))
curdoc().add_periodic_callback(update, 1000)
curdoc().title = "Server Logs"