Skip to content

How to update table.columns correctly? #4418

Closed Answered by falkoschindler
weinibuliu asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @weinibuliu,

This is indeed unexpected. I guess the problem is that ui.table(columns=a, ...) keeps a reference on a. When writing to the property like table.columns = b, the content of the original reference is overwritten by b.

Minimum reproduction:

a = [{"name": "a", "label": "A", "field": "a"}]
b = [{"name": "b", "label": "B", "field": "b"}]

table = ui.table(columns=a, rows=[{"a": "A1", "b": "B1"}, {"a": "A2", "b": "B2"}])
ui.checkbox("Toggle", on_change=lambda v: toggle(v.value))

def toggle(show_b: bool):
    table.columns = b if show_b else a

A quick solution is to pass a copy of the original columns like this:

table = ui.table(columns=a[:], rows=[{"a": "A1", "b": "B1"}, {"a": "A2"

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@weinibuliu
Comment options

Answer selected by weinibuliu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants