Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions nicegui/elements/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ValueChangeEventArguments,
handle_event,
)
from ..logging import log
from .mixins.filter_element import FilterElement

if importlib.util.find_spec('pandas'):
Expand Down Expand Up @@ -79,6 +80,8 @@ def __init__(self,
self._selection_handlers = [on_select] if on_select else []
self._pagination_change_handlers = [on_pagination_change] if on_pagination_change else []

self._scan_rows_for_lists()

def handle_selection(e: GenericEventArguments) -> None:
if e.args['added']:
if self.selection == 'single':
Expand All @@ -100,6 +103,25 @@ def handle_pagination_change(e: GenericEventArguments) -> None:
handle_event(handler, arguments)
self.on('update:pagination', handle_pagination_change)

def _scan_rows_for_lists(self) -> None:
"""Check if any cell in the rows contains a list.

Also catches anything that is eventually converted to a list in the conversion to JSON."""
for row in self._props['rows']:
for key, value in row.items():
if isinstance(value, (list, set, tuple)) and f'body-cell-{key}' not in self.slots:
log.warning(f'Found list in row in column "{key}": {value}.\n'
'Unless there is slot template, '
'table rows must not contain lists. '
'or the browser will crash.\n'
'NiceGUI is intervening by adding a slot template to display the list as comma-separated values.\n'
)
self.add_slot(f'body-cell-{key}', '''
<td class="text-right" :props="props">
{{ Array.isArray(props.value) ? props.value.join(', ') : props.value }}
</td>
''')

def on_select(self, callback: Handler[TableSelectionEventArguments]) -> Self:
"""Add a callback to be invoked when the selection changes."""
self._selection_handlers.append(callback)
Expand Down Expand Up @@ -283,6 +305,7 @@ def rows(self) -> list[dict]:
@rows.setter
def rows(self, value: list[dict]) -> None:
self._props['rows'] = value
self._scan_rows_for_lists()

@property
def columns(self) -> list[dict]:
Expand Down