[从最近开始,我一直试图使用Bokeh绘制实时数据并显示在.html上,以便嵌入到网页中。我已经成功地将其中一个bokeh示例调整为我的需要。我在情节上使用了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
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应用程序中,或者制成以python
而不是bokeh serve
运行的“常规” python脚本。有关更多信息,请参见《用户指南》中的Running a Bokeh Server。
您如何解决您的问题?我的问题和我们的一样!你能解释一下你是怎么做的吗? plz ..非常感谢〜