2016-06-28 54 views
1

到線圖圖我沒能獲得任何形式的輸出上運行的代碼(空箱)的..無法使用背景虛化流

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" 

請幫我一下,我要去哪裏錯了?

回答

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"