Bokeh表格与字典嵌套字典

4
我将尝试使用bokeh绘制一个表格,以表示一个字典的字典,但是我无法弄清如何完成。 下面是我的简单代码:
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn

def testTable():
    data = {'position1': {'x': 0, 'y': 0, 'z': 0},
            'position2': {'x': 1.1, 'y': 2.3, 'z': 3},
            'position3': {'x': 2.9, 'y': 4.3, 'z': 3.1}}           

    source = ColumnDataSource(data)

    columns = [
            TableColumn(field="x", title="x"),
            TableColumn(field="y", title="y"),
            TableColumn(field="z", title="z"),
        ]
    data_table = DataTable(source=source, columns=columns, width=400, height=280)

    plot = column(widgetbox(data_table))

    show(plot)

当我运行这段代码时,我会收到以下错误提示:
ValueError: expected an element of ColumnData(String, Seq(Any)), got {'position2': {'y': 2.3, 'x': 1.1, 'z': 3}, 'position3': {'y': 4.3, 'x': 2.9, 'z': 3.1}, 'position1': {'y': 0, 'x': 0, 'z': 0}}

我想要做的是创建一个像这样的表格:

           x    y    z
position1  0    0    0
position2  1.1  2.3  3
position3  2.9  4.3  3.1

什么是正确的做法?
编辑:
通过以不同的方式重构输入数据,我能够获得一些东西:
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn
from bokeh.layouts import widgetbox, column
from bokeh.io import output_file, show

def testTable():
    data = {'InitPosition': ['position1', 'position2', 'position3'],
            'x': [0,0,0],
            'y': [1.1, 2.3, 3],
            'z': [2.9, 4.3, 3.1]}           

    source = ColumnDataSource(data)

    columns = [
            TableColumn(field="InitPosition", title="Init Position"),
            TableColumn(field="x", title="x"),
            TableColumn(field="y", title="y"),
            TableColumn(field="z", title="z")
        ]
    data_table = DataTable(source=source, columns=columns, width=400, height=280)

    plot = column(widgetbox(data_table))

    show(plot)

if __name__ == '__main__':
    testTable()

然而,生成的表格看起来像这样:

Bokeh table

所以新问题是:如何摆脱自动生成的第一个表格?
1个回答

2

有一个布尔属性row_headers,您可以将其传递给DataTable以启用/禁用行标题,即索引列。

您可以尝试以下内容:

data_table = DataTable(source=source, columns=columns, width=400, height=280, row_headers=False)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接