动态地将列分配给Bokeh数据表

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

我有一个由ColumnDataSource()驱动的数据表。

我的列标题是根据所选数据动态分配的。

我有一个Select()下拉菜单设置与dropdown.select.on_change()和更新功能我拉新数据并分配使用:

table.data = {}

但是我想在on_change()调用期间更改列名。

我没有看到在文档中完成此操作的简单方法。

bokeh
1个回答
1
投票

没有官方支持,但您可以像这样为表格分配新列(适用于Bokeh v1.0.4)。与bokeh serve --show app.py一起运行

from random import random
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, DataTable, TableColumn, StringEditor, IntEditor, NumberEditor

source = ColumnDataSource(dict(index = [0], NAME = ["bar"], ID = [1], S1 = [2]))

original_columns = [
    TableColumn(field = "NAME", title = "Name", editor = StringEditor(), name = 'custom_header_name'),
    TableColumn(field = "ID", title = "ID", editor = IntEditor()),
    TableColumn(field = "S1", title = "Value", editor = NumberEditor()) ]

modyfied_columns = [
    TableColumn(field = "NAME", title = "Foo", editor = StringEditor(), name = 'custom_header_name'),
    TableColumn(field = "ID", title = "Bar", editor = IntEditor()),
    TableColumn(field = "S1", title = "Oeps", editor = NumberEditor()) ]

data_table = DataTable(source = source, width = 1000, height = 80, columns = original_columns)

data_table.height = 2000
i = 0

def update_data():
    global i, data_table, original_columns

    if i % 2 == 0:
        data_table.columns = modyfied_columns
    else:
        data_table.columns = original_columns

    NAME, ID, S1 = "foo", i, random()
    i += 1

    data = dict(index = [i], NAME = [NAME], ID = [ID], S1 = [S1])
    source.stream(data, 500)

curdoc().add_root(data_table)
curdoc().add_periodic_callback(update_data, 500)

结果;

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.