Python 如何在每次数据更改时实时更新flask应用程序中的bokeh图表?

Python 如何在每次数据更改时实时更新flask应用程序中的bokeh图表?,python,flask,bokeh,Python,Flask,Bokeh,如果我有一个flask应用程序,其中有几张由bokeh制作的图表,那么在添加或更改新数据时,如何在浏览器中实时更新图表?我是bokeh的新手,阅读了我必须启动bokeh服务器的文档,但我仍然不明白应该向代码中添加什么以使其更新 这是一个Bokeh服务器应用程序,每1s执行一次回调,每5s更新一次数据。查看当前绘图数据长度,并将其与最后一个绘图数据进行比较,让我们确定何时需要更新绘图(代码适用于Bokeh v1.0.4) 结果: import random, time from tornado.

如果我有一个flask应用程序,其中有几张由bokeh制作的图表,那么在添加或更改新数据时,如何在浏览器中实时更新图表?我是bokeh的新手,阅读了我必须启动bokeh服务器的文档,但我仍然不明白应该向代码中添加什么以使其更新

这是一个Bokeh服务器应用程序,每1s执行一次回调,每5s更新一次数据。查看当前
绘图数据
长度,并将其与
最后一个绘图数据
进行比较,让我们确定何时需要更新绘图(代码适用于Bokeh v1.0.4)

结果:

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()