bokeh - 到嵌入式 Bokeh 服务器的异步流式传输
问题描述
问题:我的 python 应用程序启动了一个 Bokeh 服务器,如本文所述:
http://matthewrocklin.com/blog/work/2017/06/28/simple-bokeh-server
现在,我想可视化流数据,它是在 python 应用程序中生成的,它启动了散景服务器并将其异步推送到我的散景可视化。这可能吗?如何?
解决方案
对的,这是可能的。我认为最好的选择是有一个单独的线程来填充数据桶,另一方面是 Bokeh 定期更新功能(如您提到的示例),它访问该数据并将其流式传输到浏览器。请参阅下面的这个简单示例。但也请查看 Bokeh 文档,了解从线程更新绘图数据。
import random, time
from tornado.ioloop import IOLoop
from bokeh.server.server import Server
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
from bokeh.plotting import figure, ColumnDataSource
from threading import Thread
class BokehApp():
plot_data = []
last_data_length = None
def __init__(self):
thread = Thread(target = self.startDataAcquisition)
thread.start()
io_loop = IOLoop.current()
server = Server(applications = {'/myapp': Application(FunctionHandler(self.make_document))}, io_loop = io_loop, port = 5001)
server.start()
server.show('/myapp')
io_loop.start()
def startDataAcquisition(self):
while True:
self.plot_data.append({'x': [random.random()], 'y': [random.random()], 'color': [random.choice(['red', 'blue', 'green'])]})
time.sleep(5)
def make_document(self, doc):
source = ColumnDataSource({'x': [], 'y': [], 'color': []})
fig = figure(title = 'Streaming Circle Plot!', sizing_mode = 'scale_both')
fig.circle(source = source, x = 'x', y = 'y', color = 'color', size = 10)
def update():
if self.last_data_length is not None and self.last_data_length != len(self.plot_data):
source.stream(self.plot_data[-1])
self.last_data_length = len(self.plot_data)
doc.add_root(fig)
doc.add_periodic_callback(update, 1000)
if __name__ == '__main__':
app = BokehApp()
推荐阅读
- angular - 离子电容器文件系统 api 在内部存储中写入个人文件夹
- django - 我在尝试保存我的 Django 表单时收到 Key 错误消息。我无法将 Django 表单中的数据保存到数据库
- ruby-on-rails - 编辑表单呈现空白
- python - 在python中将str格式化为浮点数并从十进制1增加到2进制
- amazon-redshift - 如何检查 Redshift 维护时段
- next.js - 在 Next.js 项目中的 Tailwind 中配置颜色会破坏所有颜色
- vuejs2 - Vue 2 和 Video.js 在视频播放时暂停视频
- c# - SQL Server 中的 NEXT VALUE FOR @Sequence 的工作方式是否与 C# 中的 Interlocked.Increment() 相同?
- electron - 命令失败,无法获取本地颁发者证书
- angular - 谷歌图表:错误类型错误:“c 未定义”