Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to clear columns in DataTable.clear() #1427

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.8.3] - Unreleased

### Added

- Added an option to clear columns in DataTable.clear() https://github.com/Textualize/textual/pull/1427

## [0.8.2] - 2022-12-28

### Fixed
Expand Down
4 changes: 3 additions & 1 deletion src/textual/widgets/_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def _get_cell_region(self, row_index: int, column_index: int) -> Region:
cell_region = Region(x, y, width, height)
return cell_region

def clear(self) -> None:
def clear(self, columns: bool = False) -> None:
"""Clear the table.
Args:
Expand All @@ -323,6 +323,8 @@ def clear(self) -> None:
self._y_offsets.clear()
self.data.clear()
self.rows.clear()
if columns:
self.columns.clear()
self._line_no = 0
self._require_update_dimensions = True
self.refresh()
Expand Down
21 changes: 21 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from rich.text import Text

from textual.app import App, ComposeResult
from textual.widgets import DataTable

Expand All @@ -18,13 +20,32 @@ async def test_table_clear() -> None:
table.add_columns("foo", "bar")
assert table.row_count == 0
table.add_row("Hello", "World!")
assert [col.label for col in table.columns] == [Text("foo"), Text("bar")]
assert table.data == {0: ["Hello", "World!"]}
assert table.row_count == 1
table.clear()
assert [col.label for col in table.columns] == [Text("foo"), Text("bar")]
assert table.data == {}
assert table.row_count == 0


async def test_table_clear_with_columns() -> None:
"""Check DataTable.clear(columns=True)"""

app = TableApp()
async with app.run_test() as pilot:
table = app.query_one(DataTable)
table.add_columns("foo", "bar")
assert table.row_count == 0
table.add_row("Hello", "World!")
assert [col.label for col in table.columns] == [Text("foo"), Text("bar")]
assert table.data == {0: ["Hello", "World!"]}
assert table.row_count == 1
table.clear(columns=True)
assert [col.label for col in table.columns] == []
assert table.data == {}
assert table.row_count == 0

async def test_table_add_row() -> None:

app = TableApp()
Expand Down