|
4 | 4 | from dataclasses import dataclass |
5 | 5 | from itertools import chain, zip_longest |
6 | 6 | 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 | +) |
8 | 17 |
|
9 | 18 | import rich.repr |
10 | 19 | from rich.console import RenderableType |
@@ -1716,20 +1725,41 @@ def add_row( |
1716 | 1725 | self.check_idle() |
1717 | 1726 | return row_key |
1718 | 1727 |
|
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. |
1721 | 1732 |
|
1722 | 1733 | 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 |
1724 | 1737 |
|
1725 | 1738 | Returns: |
1726 | 1739 | A list of the keys for the columns that were added. See |
1727 | 1740 | the `add_column` method docstring for more information on how |
1728 | 1741 | 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 | + ``` |
1729 | 1755 | """ |
1730 | 1756 | 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) |
1733 | 1763 | column_keys.append(column_key) |
1734 | 1764 | return column_keys |
1735 | 1765 |
|
|
0 commit comments