Skip to content

Commit 03bef71

Browse files
authored
Merge pull request #5923 from Hibbins/feature/add-columns-tuple-support
Support for tuple keys in add_columns function
2 parents 38ca1dd + 7c72cfc commit 03bef71

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2626
- Added `compact` to Binding.Group https://github.com/Textualize/textual/pull/6132
2727
- Added `Screen.get_hover_widgets_at` https://github.com/Textualize/textual/pull/6132
2828
- Added `Content.wrap` https://github.com/Textualize/textual/pull/6138
29+
- Added support to allow support for manual keys in add_columns as well. https://github.com/Textualize/textual/pull/5923
2930

3031
### Fixed
3132

src/textual/widgets/_data_table.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
from dataclasses import dataclass
55
from itertools import chain, zip_longest
66
from operator import itemgetter
7-
from typing import Any, Callable, ClassVar, Generic, Iterable, NamedTuple, TypeVar
7+
from typing import (
8+
Any,
9+
Callable,
10+
ClassVar,
11+
Generic,
12+
Iterable,
13+
NamedTuple,
14+
TypeVar,
15+
Union,
16+
)
817

918
import rich.repr
1019
from rich.console import RenderableType
@@ -1725,20 +1734,41 @@ def add_row(
17251734
self.check_idle()
17261735
return row_key
17271736

1728-
def add_columns(self, *labels: TextType) -> list[ColumnKey]:
1729-
"""Add a number of columns.
1737+
def add_columns(
1738+
self, *columns: Union[TextType, tuple[TextType, str]]
1739+
) -> list[ColumnKey]:
1740+
"""Add multiple columns to the DataTable.
17301741
17311742
Args:
1732-
*labels: Column headers.
1743+
*columns: Column specifications. Each can be either:
1744+
- A string or Text object (label only, auto-generated key)
1745+
- A tuple of (label, key) for manual key control
17331746
17341747
Returns:
17351748
A list of the keys for the columns that were added. See
17361749
the `add_column` method docstring for more information on how
17371750
these keys are used.
1751+
1752+
Examples:
1753+
```python
1754+
# Add columns with auto-generated keys
1755+
keys = table.add_columns("Name", "Age", "City")
1756+
1757+
# Add columns with manual keys
1758+
keys = table.add_columns(
1759+
("Name", "name_col"),
1760+
("Age", "age_col"),
1761+
"City" # Mixed with auto-generated key
1762+
)
1763+
```
17381764
"""
17391765
column_keys = []
1740-
for label in labels:
1741-
column_key = self.add_column(label, width=None)
1766+
for column in columns:
1767+
if isinstance(column, tuple):
1768+
label, key = column
1769+
column_key = self.add_column(label, width=None, key=key)
1770+
else:
1771+
column_key = self.add_column(column, width=None)
17421772
column_keys.append(column_key)
17431773
return column_keys
17441774

tests/test_data_table.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,25 @@ async def test_add_columns():
299299
assert len(table.columns) == 3
300300

301301

302+
async def test_add_columns_with_tuples():
303+
app = DataTableApp()
304+
async with app.run_test():
305+
table = app.query_one(DataTable)
306+
column_keys = table.add_columns(
307+
("Column 1", "col1"), "Column 2", ("Column 3", "col3")
308+
)
309+
assert len(column_keys) == 3
310+
assert len(table.columns) == 3
311+
312+
assert column_keys[0] == "col1"
313+
assert column_keys[1] != "col1"
314+
assert column_keys[2] == "col3"
315+
316+
assert table.columns[column_keys[0]].label.plain == "Column 1"
317+
assert table.columns[column_keys[1]].label.plain == "Column 2"
318+
assert table.columns[column_keys[2]].label.plain == "Column 3"
319+
320+
302321
async def test_add_columns_user_defined_keys():
303322
app = DataTableApp()
304323
async with app.run_test():

0 commit comments

Comments
 (0)