向Bokeh ColumnDataSource输入字典列表时出错

时间:2019-06-21 19:20:28

标签: python data-visualization bokeh

我正在使用Bokeh绘制一些来自JSON Blob(通过Python检索)的数据的图形。我想将原始JSON数据传递到ColumnDataSource中,以便当用户单击某个点时,将显示原始JSON Blob中的所有数据(使用CustomJS工具提示)。但是,当我执行以下操作时,出现'Uncaught TypeError:Can not read property'0'of undefined'

设置图形:

source = ColumnDataSource(data=dict(
    x = xvals, # list of datetimes
    y = yvals, # list of strings (categorical variable),
    info = info, # list of dicts (each dict has the same 7 keys)
))

JS显示部分:

code = """
    selection = require("core/util/selection")
    indices = selection.get_indices(source)
    for (i = 0; i < indices.length; i++) {
        ind = indices[i]
        document.getElementById('info').style.display = 'block'
        document.getElementById('info').innerHTML= source.data['info']
    }
    """

所有三个列表的长度相同。我已经尝试过了,并且可以使用列表列表,而不是字典列表。有没有人能够使这个工作/关于如何解决这个问题的任何建议?我目前正在将字典列表转换为7个单独的列表,但强烈不希望这样做,因为我需要此代码才能扩展到更大的JSON数据集。

或者,还有另一种方法(除了通过ColumnDataSource传递数据之外),以允许CustomJS代码从Python读取JSON数据?谢谢!

1 个答案:

答案 0 :(得分:0)

我的建议是在每个JSON Blob上调用json.dumps,以便您创建的是字符串列,而不是字典列。如果仅显示信息,则该信息已经是字符串。如果实际上在回调等中需要该结构,则可以在其上调用JSON.parse