散景+烧瓶:多个AjaxDataSource调用 [英] Bokeh+Flask: Multiple AjaxDataSource calls

查看:53
本文介绍了散景+烧瓶:多个AjaxDataSource调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

散景对象是显示3条独立线的图形.

A Bokeh object is a figure that shows 3 indipendent lines.

流式传输数据. AjaxDataSource调用每5秒钟更新一次数据,从数据库中读取最后一个.

Data are streamed. An AjaxDataSource call updates the data every 5 seconds reading the last ones from a database.

这是精简的课程:

class Graph:
    def __init__(self):

        data_source = AjaxDataSource(data=dict(date_time=[],
                                               value_1=[], 
                                               value_2=[], 
                                               value_3=[]), 
                                               data_url="/data",
                                               polling_interval=5000)

        line1 = self.figure.line(x="date_time", y="value_1", source=data_source)
        line2 = self.figure.line(x="date_time", y="value_2", source=data_source)
        line3 = self.figure.line(x="date_time", y="value_3", source=data_source)

        app.add_url_rule("/data", "/data", self.serve, methods=['GET', 'OPTIONS', 'POST'])

    def serve(self):

        # load data from db and return JSON
        ...
        return jsonify(
            date_time= da.df["Date_Time"].tolist(),
            value_1=da.df["Value1"].tolist(),
            value_2=da.df["Value2"].tolist(),
            value_3=da.df["Value3"].tolist()
        )

date_time是公共的x轴,value_1是第1行,value_2是第2行,value_3是第3行.

date_time is the common x-axis, value_1 is for line 1, value_2 for line 2, value_3 for line 3.

问题

为什么AjaxDataSource被调用3次(相隔几毫秒,然后在5秒后再次读取三次)而不是每5秒一次?

Why is AjaxDataSource called 3 times (they are a few milliseconds apart, then the triple reading is done again after 5 seconds) instead of only once every 5 seconds?

我相信AjaxDataSource每5秒动态填充一次data_source.data,然后在读取它们之后,三行将读取这些现在是静态的"数据.

I believed that AjaxDataSource fills dynamically data_source.data every 5 seconds and then, after they have been read, the 3 lines read these "now static" data.

解决方法?

是否可以使用AjaxDataSource读取数据,自动将数据传输到ColumnDataSource并将其用作静态"数据源?

Is there a way to read the data using AjaxDataSource, transfer automatically the data into a ColumnDataSource and use this as a "static" data source?

或者我在这里错过了重要的事情吗?

Or am I missing something important here?

推荐答案

问题是,每个附加了远程数据源的字形都试图初始化数据源.对于AjaxDataSource,它不检查以前的初始化.

The issue is that each glyph that has a remote data source attached to it, tries to initialize the data source. In the case of AjaxDataSource, it doesn't check for previous initializations.

我为此打开了一个问题: https://github.com/bokeh/bokeh/issues 6736

I've opened an issue for it: https://github.com/bokeh/bokeh/issues/6736

您可以尝试的临时解决方法:

A temporary workaround that you could try:

Bokeh.require('models/sources/ajax_data_source').AjaxDataSource.prototype.setup = function () {
    this.get_data(this.mode);
    if (this.polling_interval && this.interval == null) {
        return this.interval = setInterval(this.get_data, this.polling_interval, this.mode, this.max_size, this.if_modified);
    }
}

确保在页面中包含Bokeh之后但在初始化其文档之前立即运行此代码.怎么做取决于您如何嵌入Bokeh.

Make sure that this code is run right after Bokeh is included in your page, but before it initializes its documents. How to do it depends on how you embed Bokeh.

这篇关于散景+烧瓶:多个AjaxDataSource调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆