Skip to content

Commit ddf64a4

Browse files
committed
feat(widgets:_data_table.py): add support for tuple keys in add_columns function
1 parent 03979fc commit ddf64a4

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

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
@@ -1716,20 +1725,41 @@ def add_row(
17161725
self.check_idle()
17171726
return row_key
17181727

1719-
def add_columns(self, *labels: TextType) -> list[ColumnKey]:
1720-
"""Add a number of columns.
1728+
def add_columns(
1729+
self, *columns: Union[TextType, tuple[TextType, str]]
1730+
) -> list[ColumnKey]:
1731+
"""Add multiple columns to the DataTable.
17211732
17221733
Args:
1723-
*labels: Column headers.
1734+
*columns: Column specifications. Each can be either:
1735+
- A string or Text object (label only, auto-generated key)
1736+
- A tuple of (label, key) for manual key control
17241737
17251738
Returns:
17261739
A list of the keys for the columns that were added. See
17271740
the `add_column` method docstring for more information on how
17281741
these keys are used.
1742+
1743+
Examples:
1744+
```python
1745+
# Add columns with auto-generated keys
1746+
keys = table.add_columns("Name", "Age", "City")
1747+
1748+
# Add columns with manual keys
1749+
keys = table.add_columns(
1750+
("Name", "name_col"),
1751+
("Age", "age_col"),
1752+
"City" # Mixed with auto-generated key
1753+
)
1754+
```
17291755
"""
17301756
column_keys = []
1731-
for label in labels:
1732-
column_key = self.add_column(label, width=None)
1757+
for column in columns:
1758+
if isinstance(column, tuple):
1759+
label, key = column
1760+
column_key = self.add_column(label, width=None, key=key)
1761+
else:
1762+
column_key = self.add_column(column, width=None)
17331763
column_keys.append(column_key)
17341764
return column_keys
17351765

0 commit comments

Comments
 (0)