嵌入式Bokeh服务器的异步流

问题描述 投票:0回答:1

问题:我的python应用程序启动了本文中描述的Bokeh服务器:

http://matthewrocklin.com/blog/work/2017/06/28/simple-bokeh-server

现在,我想要可视化流数据,这是在python应用程序中生成的,它启动了散景服务器并将其异步推送到我的散景可视化。这可能吗?怎么样?

bokeh
1个回答
1
投票

对的,这是可能的。我认为最好的选择是有一个单独的线程填充数据桶,另一方面是Bokeh定期更新功能(如你提到的例子)访问该数据并将其流式传输到浏览器。请看下面这个简单的例子。但是还要看看关于updating plot data from threads的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()
© www.soinside.com 2019 - 2024. All rights reserved.