Display table works for nicegui<=1.4.37, but not for nicegui>=2.0 #4297
-
QuestionThe following code works fine for nicegui 1.4.37 import numpy as np
from nicegui import ui
def format_table_from_array(data):
if len(data.shape) == 1:
data = np.expand_dims(np.asarray(data), 1)
(height, width) = data.shape[:2]
if width > height:
data = np.transpose(data)
(height, width) = data.shape[:2]
if height > 1000 and width > 1000:
rows, columns = None, None
else:
fm_data = np.insert(data, 0, np.arange(height), axis=1)
columns = [{"name": "Column " + str(j), "label": "Column " + str(j),
"field": "Column " + str(j)} for j in range(width)]
columns.insert(0,
{"name": "Index", "label": "Index", "field": "Index"})
rows = []
for i in range(height):
dict_tmp = {}
for j in range(width + 1):
dict_tmp[columns[j]["name"]] = fm_data[i][j]
rows.append(dict_tmp)
return rows, columns
def show_table():
data = np.random.rand(10, 1)
rows, columns = format_table_from_array(data)
if table.rows is None:
table._props['rows'] = rows
table._props['columns'] = columns
else:
table.rows[:] = rows
table.update()
rows, columns = None, None
ui.button('Show data', on_click=lambda: show_table())
table = ui.table(columns=columns, rows=rows, row_key="Index")
ui.run() |
Beta Was this translation helpful? Give feedback.
Answered by
falkoschindler
Feb 2, 2025
Replies: 1 comment 1 reply
-
Hi @nghia-vo, In NiceGUI 1.x table = ui.table(columns=[], rows=[], row_key="Index") Later you can update them like this: table.rows[:] = rows
table.columns[:] = columns
table.update() |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nghia-vo
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @nghia-vo,
In NiceGUI 1.x
ui.table
didn't expectcolumns=None
orrows=None
. So your code relied on undefined behavior. And while 2.0 added support forcolumns=None
,rows=None
is still invalid. Instead you should use empty lists:Later you can update them like this: