简体   繁体   中英

not able to plot line graph using bokeh streaming

I am not able to get any sort of output(blank box) on running this code..

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"

Please help me as to Where am I going wrong?

By adding the follow interval to the stream callback the code below works using bokeh version 0.11.1.

Note: Without a copy of the f file the streaming data had to be randomly generated in the callback.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM