TableColumn visible property is not working if a column is added after the DataTable is created

Hi,
I need to add TableColumn to a DataTable after the table was created. The column gets added and shown (see the minimal example added).
I am expecting to be able to change the visibility of this new column but when I change the “visible” property nothing happens (The column stays visible at all time).

Could you tell me if it is possible to do? If not is there a workaround?

Minimal Bokeh Server Example:

from bokeh.models import ColumnDataSource
from bokeh.models import DataTable
from bokeh.models import TableColumn
from bokeh.models import NumberFormatter
from bokeh.models import Toggle
from bokeh.layouts import row
from bokeh.plotting import show
from bokeh.io import curdoc


source = ColumnDataSource(data=dict())
columns = {
    "name": TableColumn(field="name", title="Employee Name"),
    "salary": TableColumn(field="salary", title="Income", formatter=NumberFormatter(format="$0,0.00")),
    "years_experience": TableColumn(field="years_experience", title="Experience (years)"),
}

data_table = DataTable(source=source, columns=list(columns.values()), width=800)


toggle1 = Toggle(label='add column')
def cb1(attr, old, new):
    if 'test' not in columns:
        source.data['test'] = ['test1']
        columns['test'] = TableColumn(field="test", title="test")
        data_table.columns.append(columns["test"])
toggle1.on_change('active', cb1)

toggle2 = Toggle(label='column visibility')
def cb2(attr, old, new):
    if 'test' in columns:
        columns['test'].visible = toggle2.active
toggle2.on_change('active', cb2)


layout=row(data_table, toggle1, toggle2)


curdoc().add_root(layout)
curdoc().title = "Data-Table Bokeh Server"

source.data = {
    'name'             : ['name1'],
    'salary'           : [20],
    'years_experience' : [15],
}

Bokeh Info:

Python version : 3.11.5 | packaged by Anaconda, Inc. | (main, Sep 11 2023, 13:26:23) [MSC v.1916 64 bit (AMD64)]
IPython version : 8.17.2
Tornado version : 6.3.3
Bokeh version : 3.4.1
BokehJS static path : C:\Users\gilles.faure\AppData\Local\miniconda3\envs\EO_py3.11\Lib\site-packages\bokeh\server\static
node.js version : (not installed)
npm version : (not installed)
jupyter_bokeh version : (not installed)
Operating system : Windows-10-10.0.19045-SP0

Perhaps it’s a bug related to the specific case of adding columns later. The simplest workaround I can think of would be to add all expected columns up front and make some invisible from the start if you don’t want to see them at first.

I thought about this workaround but the problem with it, is that we need to know how many columns are expected. In our use case we do not know the expected number of columns.

How difficult would it be to fix it? Can a bug request be raised?

You can definitely submit a GitHub Issue with full details. I have no idea how difficult it will be to fix or when it might make it into a release, there’s another core dev that currently handles most BokehJS issues.

I raise the issue:
[BUG] TableColumn visible property is not working if a column is added after the DataTable is created · Issue #13857 · bokeh/bokeh (github.com)

Thanks Bryan

1 Like