Skip to content

Commit

Permalink
Merge pull request #2132 from steweg/feature/enhanced_table_api
Browse files Browse the repository at this point in the history
Enhancing API of table element
  • Loading branch information
falkoschindler authored Dec 15, 2023
2 parents 6f90660 + f79bd57 commit 27c6602
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 11 additions & 0 deletions nicegui/elements/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ def remove_rows(self, *rows: Dict) -> None:
self.selected[:] = [row for row in self.selected if row[self.row_key] not in keys]
self.update()

def update_rows(self, rows: List[Dict], *, clear_selection: bool = True) -> None:
"""Update rows in the table.
:param rows: list of rows to update
:param clear_selection: whether to clear the selection (default: True)
"""
self.rows[:] = rows
if clear_selection:
self.selected.clear()
self.update()

class row(Element):

def __init__(self) -> None:
Expand Down
16 changes: 13 additions & 3 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,32 @@ def test_remove_selection(screen: Screen):
def test_replace_rows(screen: Screen):
t = ui.table(columns=columns(), rows=rows())

def replace_rows():
def replace_rows_with_carol():
t.rows = [{'id': 3, 'name': 'Carol', 'age': 32}]
ui.button('Replace rows', on_click=replace_rows)

def replace_rows_with_daniel():
t.update_rows([{'id': 4, 'name': 'Daniel', 'age': 33}])

ui.button('Replace rows with C.', on_click=replace_rows_with_carol)
ui.button('Replace rows with D.', on_click=replace_rows_with_daniel)

screen.open('/')
screen.should_contain('Alice')
screen.should_contain('Bob')
screen.should_contain('Lionel')

screen.click('Replace rows')
screen.click('Replace rows with C.')
screen.wait(0.5)
screen.should_not_contain('Alice')
screen.should_not_contain('Bob')
screen.should_not_contain('Lionel')
screen.should_contain('Carol')

screen.click('Replace rows with D.')
screen.wait(0.5)
screen.should_not_contain('Carol')
screen.should_contain('Daniel')


def test_create_from_pandas(screen: Screen):
df = pd.DataFrame({'name': ['Alice', 'Bob'], 'age': [18, 21], 42: 'answer'})
Expand Down

0 comments on commit 27c6602

Please sign in to comment.