Skip to content

Commit e59d606

Browse files
authored
Merge pull request #1427 from JoshKarpel/clear-table-cols
Add option to clear columns in `DataTable.clear()`
2 parents cef386a + a45a3dc commit e59d606

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [0.8.3] - Unreleased
9+
10+
### Added
11+
12+
- Added an option to clear columns in DataTable.clear() https://github.com/Textualize/textual/pull/1427
13+
814
## [0.8.2] - 2022-12-28
915

1016
### Fixed

src/textual/widgets/_data_table.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ def _get_cell_region(self, row_index: int, column_index: int) -> Region:
312312
cell_region = Region(x, y, width, height)
313313
return cell_region
314314

315-
def clear(self) -> None:
315+
def clear(self, columns: bool = False) -> None:
316316
"""Clear the table.
317317
318318
Args:
@@ -323,6 +323,8 @@ def clear(self) -> None:
323323
self._y_offsets.clear()
324324
self.data.clear()
325325
self.rows.clear()
326+
if columns:
327+
self.columns.clear()
326328
self._line_no = 0
327329
self._require_update_dimensions = True
328330
self.refresh()

tests/test_table.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from rich.text import Text
4+
35
from textual.app import App, ComposeResult
46
from textual.widgets import DataTable
57

@@ -18,13 +20,32 @@ async def test_table_clear() -> None:
1820
table.add_columns("foo", "bar")
1921
assert table.row_count == 0
2022
table.add_row("Hello", "World!")
23+
assert [col.label for col in table.columns] == [Text("foo"), Text("bar")]
2124
assert table.data == {0: ["Hello", "World!"]}
2225
assert table.row_count == 1
2326
table.clear()
27+
assert [col.label for col in table.columns] == [Text("foo"), Text("bar")]
2428
assert table.data == {}
2529
assert table.row_count == 0
2630

2731

32+
async def test_table_clear_with_columns() -> None:
33+
"""Check DataTable.clear(columns=True)"""
34+
35+
app = TableApp()
36+
async with app.run_test() as pilot:
37+
table = app.query_one(DataTable)
38+
table.add_columns("foo", "bar")
39+
assert table.row_count == 0
40+
table.add_row("Hello", "World!")
41+
assert [col.label for col in table.columns] == [Text("foo"), Text("bar")]
42+
assert table.data == {0: ["Hello", "World!"]}
43+
assert table.row_count == 1
44+
table.clear(columns=True)
45+
assert [col.label for col in table.columns] == []
46+
assert table.data == {}
47+
assert table.row_count == 0
48+
2849
async def test_table_add_row() -> None:
2950

3051
app = TableApp()

0 commit comments

Comments
 (0)