刷新后Bokeh实时更新x_axis

时间:2018-01-19 09:43:26

标签: python real-time visualization updates bokeh

从最近几天开始,我一直在尝试使用Bokeh绘制实时数据并在.html上显示,以便嵌入到网页中。我已经根据自己的需要调整了一个散景例子。我在剧情上使用了50个元素的缓冲区,我注意到以下行为:

1)如果我运行脚本并转到浏览器,x_range完全适应于收集数据并且一切正常

2)如果我点击"刷新"在浏览器上,x_range停止以适应传入的数据并冻结到最后一个值。

我试图强制x_axis为初始值和结束值,但可视化效果不佳。

我认为我没有正确理解"刷新"命中影响我的代码以及我如何解决这个问题。

""" To view this example, first start a Bokeh server:

bokeh serve --allow-websocket-origin=localhost:8000

And then load the example into the Bokeh server by
running the script:

python animated.py

in this directory. Finally, start a simple web server
by running:

python -m SimpleHTTPServer  (python 2)

or

python -m http.server  (python 3)

in this directory. Navigate to

http://localhost:8000/animated.html

"""
from __future__ import print_function

import io

from numpy import pi, cos, sin, linspace, roll

from bokeh.client import push_session
from bokeh.embed import server_session
from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource

fa = open('Accelerometer.txt', 'r')

source = ColumnDataSource(data=dict(x=[], y=[]))
fg = figure(width=250, plot_height=250, title="RT-Test")
fg.line(x='x', y='y', color="olive", source=source)
fg.x_range.follow = "end"

# Visualization scale and aesthetics
fg.xgrid.grid_line_color = None
fg.ygrid.grid_line_color = None
fg.background_fill_color = "snow"

# add the plot to curdoc
curdoc().add_root(fg)

# open a session which will keep our local doc in sync with server
session = push_session(curdoc())

html = """
 <html>
  <head></head>
  <body>
    %s
  </body>
</html>
""" % server_session(fg, session_id=session.id, relative_urls=False)

with io.open("animated.html", mode='w+', encoding='utf-8') as f:
    f.write(html)

print(__doc__)

def update():
    line = fa.readline().split(',')
    x = float(line[0])
    y = float(line[1])
    print(x, y)

    # construct the new values for all columns, and pass to stream
    new_data = dict(x=[x], y=[y])
    source.stream(new_data, rollover=50)

curdoc().add_periodic_callback(update, 100)

session.loop_until_closed() # run forever

1 个答案:

答案 0 :(得分:1)

Bokeh服务器的这种用法,实际代码在一个单独的进程中运行并调用session.loop_until_closed 最强烈地劝阻 。在下一个版本中,将删除此类所有示例,并从文档中删除此方法的提及。这种用法本身在许多方面都是低劣的,as outlined here,我会说这么长时间地证明它如此突出是我们的错误。它偶尔会用于测试,但没有别的。

那么使用Bokeh服务器的好方法是什么?答案是让Bokeh应用程序在Bokeh服务器本身运行 ,与上面的代码不同。这可以通过多种方式完成,但是有一种常见的方法可以用来编写一个简单的脚本,然后用

执行该脚本
bokeh serve -show myapp.py

我无法访问您的“Accelerate.py”数据集,但更新代码的粗略通道如下:

# myapp.py     
from numpy import pi, cos, sin, linspace, roll

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource

fa = open('Accelerometer.txt', 'r')

source = ColumnDataSource(data=dict(x=[], y=[]))
fg = figure(width=250, plot_height=250, title="RT-Test")
fg.line(x='x', y='y', color="olive", source=source)
fg.x_range.follow = "end"

fg.xgrid.grid_line_color = None
fg.ygrid.grid_line_color = None
fg.background_fill_color = "snow"

curdoc().add_root(fg)

def update():
    line = fa.readline().split(',')
    x = float(line[0])
    y = float(line[1])

    # construct the new values for all columns, and pass to stream
    new_data = dict(x=[x], y=[y])
    source.stream(new_data, rollover=50)

curdoc().add_periodic_callback(update, 100)

现在,如果您使用bokeh serve命令运行此脚本,那么任何刷新都将为您提供此应用程序的全新会话。以这种方式编写的代码相当简单和简单,这也是值得的。

这些类型的应用程序可以嵌入Jupyter笔记本,Flask和其他Web应用程序中,或者制作成“{1}}而不是python运行的”常规“python脚本。有关详细信息,请参阅“用户指南”中的Running a Bokeh Server