0
# Tables
1
2
Comprehensive table system with flexible columns, styling, borders, and automatic sizing. Rich tables support complex layouts, custom formatting, and responsive design with automatic width calculation.
3
4
## Capabilities
5
6
### Table Class
7
8
Advanced table rendering with comprehensive formatting and layout options.
9
10
```python { .api }
11
class Table:
12
"""
13
Renderable table with automatic sizing and formatting.
14
15
Args:
16
*columns: Column definitions or header strings
17
title: Title displayed above the table
18
caption: Caption displayed below the table
19
width: Fixed table width or None for automatic
20
min_width: Minimum table width
21
box: Box style for borders
22
safe_box: Use safe box characters for compatibility
23
padding: Cell padding (top, right, bottom, left)
24
collapse_padding: Collapse padding between cells
25
pad_edge: Add padding to table edges
26
expand: Expand table to fill available width
27
show_header: Display header row
28
show_footer: Display footer row
29
show_edge: Display outer borders
30
show_lines: Display lines between rows
31
leading: Number of blank lines between rows
32
style: Default table style
33
row_styles: Alternating row styles
34
header_style: Header row style
35
footer_style: Footer row style
36
border_style: Border style
37
title_style: Title style
38
caption_style: Caption style
39
title_justify: Title justification
40
caption_justify: Caption justification
41
highlight: Enable cell content highlighting
42
"""
43
def __init__(
44
self,
45
*columns: Union[Column, str],
46
title: Optional[TextType] = None,
47
caption: Optional[TextType] = None,
48
width: Optional[int] = None,
49
min_width: Optional[int] = None,
50
box: Optional[Box] = box.HEAVY_HEAD,
51
safe_box: Optional[bool] = None,
52
padding: PaddingDimensions = (0, 1),
53
collapse_padding: bool = False,
54
pad_edge: bool = True,
55
expand: bool = False,
56
show_header: bool = True,
57
show_footer: bool = False,
58
show_edge: bool = True,
59
show_lines: bool = False,
60
leading: int = 0,
61
style: StyleType = "",
62
row_styles: Optional[Iterable[StyleType]] = None,
63
header_style: Optional[StyleType] = None,
64
footer_style: Optional[StyleType] = None,
65
border_style: Optional[StyleType] = None,
66
title_style: Optional[StyleType] = None,
67
caption_style: Optional[StyleType] = None,
68
title_justify: JustifyMethod = "center",
69
caption_justify: JustifyMethod = "center",
70
highlight: bool = False,
71
): ...
72
73
def add_column(
74
self,
75
header: RenderableType = "",
76
*,
77
footer: RenderableType = "",
78
header_style: Optional[StyleType] = None,
79
footer_style: Optional[StyleType] = None,
80
style: Optional[StyleType] = None,
81
justify: JustifyMethod = "left",
82
vertical: VerticalAlignMethod = "top",
83
overflow: OverflowMethod = "ellipsis",
84
width: Optional[int] = None,
85
min_width: Optional[int] = None,
86
max_width: Optional[int] = None,
87
ratio: Optional[int] = None,
88
no_wrap: bool = False,
89
) -> None:
90
"""
91
Add a column to the table.
92
93
Args:
94
header: Column header content
95
footer: Column footer content
96
header_style: Style for header cell
97
footer_style: Style for footer cell
98
style: Default style for column cells
99
justify: Text justification in column
100
vertical: Vertical alignment in column
101
overflow: Overflow handling method
102
width: Fixed column width
103
min_width: Minimum column width
104
max_width: Maximum column width
105
ratio: Column width ratio for flexible sizing
106
no_wrap: Disable text wrapping in column
107
"""
108
109
def add_row(
110
self,
111
*renderables: Optional[RenderableType],
112
style: Optional[StyleType] = None,
113
end_section: bool = False,
114
) -> None:
115
"""
116
Add a row to the table.
117
118
Args:
119
*renderables: Cell contents for each column
120
style: Style for the entire row
121
end_section: Mark this row as end of a section
122
"""
123
124
def add_section(self) -> None:
125
"""Add a section break (line) after the current row."""
126
127
@classmethod
128
def grid(
129
cls,
130
*,
131
padding: PaddingDimensions = 0,
132
collapse_padding: bool = True,
133
pad_edge: bool = False,
134
expand: bool = False,
135
) -> "Table":
136
"""
137
Create a table configured as a grid (no borders).
138
139
Args:
140
padding: Cell padding
141
collapse_padding: Collapse padding between cells
142
pad_edge: Add padding to grid edges
143
expand: Expand grid to fill available width
144
145
Returns:
146
New Table configured as grid
147
"""
148
149
# Properties
150
@property
151
def columns(self) -> List[Column]:
152
"""Get list of table columns."""
153
154
@property
155
def rows(self) -> List[Row]:
156
"""Get list of table rows."""
157
158
@property
159
def row_count(self) -> int:
160
"""Get number of rows in table."""
161
```
162
163
### Column Class
164
165
Column definition with formatting and sizing options.
166
167
```python { .api }
168
@dataclass
169
class Column:
170
"""
171
Table column definition.
172
173
Attributes:
174
header: Header content for the column
175
footer: Footer content for the column
176
header_style: Style for header cell
177
footer_style: Style for footer cell
178
style: Default style for column cells
179
justify: Text justification method
180
vertical: Vertical alignment method
181
overflow: Overflow handling method
182
width: Fixed column width
183
min_width: Minimum column width
184
max_width: Maximum column width
185
ratio: Width ratio for flexible sizing
186
no_wrap: Disable text wrapping
187
"""
188
header: RenderableType = ""
189
footer: RenderableType = ""
190
header_style: StyleType = ""
191
footer_style: StyleType = ""
192
style: StyleType = ""
193
justify: JustifyMethod = "left"
194
vertical: VerticalAlignMethod = "top"
195
overflow: OverflowMethod = "ellipsis"
196
width: Optional[int] = None
197
min_width: Optional[int] = None
198
max_width: Optional[int] = None
199
ratio: Optional[int] = None
200
no_wrap: bool = False
201
```
202
203
### Row Class
204
205
Table row data container.
206
207
```python { .api }
208
@dataclass
209
class Row:
210
"""
211
Table row data.
212
213
Attributes:
214
style: Style for the entire row
215
end_section: Whether this row ends a section
216
"""
217
style: Optional[StyleType] = None
218
end_section: bool = False
219
```
220
221
### Box Styles
222
223
Predefined box drawing styles for table borders.
224
225
```python { .api }
226
class Box:
227
"""Box drawing character set for table borders."""
228
229
def __init__(
230
self,
231
box: str,
232
*,
233
ascii: bool = False,
234
): ...
235
236
# Predefined box styles
237
ASCII: Box # ASCII characters only
238
SQUARE: Box # Square corners
239
MINIMAL: Box # Minimal borders
240
SIMPLE: Box # Simple lines
241
SIMPLE_HEAD: Box # Simple with header separator
242
SIMPLE_HEAVY: Box # Simple with heavy lines
243
HORIZONTALS: Box # Horizontal lines only
244
ROUNDED: Box # Rounded corners
245
HEAVY: Box # Heavy lines
246
HEAVY_EDGE: Box # Heavy outer edge
247
HEAVY_HEAD: Box # Heavy header separator
248
DOUBLE: Box # Double lines
249
DOUBLE_EDGE: Box # Double outer edge
250
```
251
252
**Usage Examples:**
253
254
```python
255
from rich.console import Console
256
from rich.table import Table, Column
257
from rich import box
258
259
console = Console()
260
261
# Basic table
262
table = Table()
263
table.add_column("Name")
264
table.add_column("Age")
265
table.add_column("City")
266
267
table.add_row("Alice", "25", "New York")
268
table.add_row("Bob", "30", "London")
269
table.add_row("Charlie", "35", "Tokyo")
270
271
console.print(table)
272
273
# Styled table with title and caption
274
table = Table(
275
title="Employee Directory",
276
caption="Last updated: 2024-01-01",
277
box=box.ROUNDED,
278
title_style="bold blue",
279
caption_style="italic dim"
280
)
281
282
table.add_column("ID", justify="center", style="cyan", no_wrap=True)
283
table.add_column("Name", style="magenta")
284
table.add_column("Department", justify="right", style="green")
285
table.add_column("Salary", justify="right", style="yellow")
286
287
table.add_row("001", "John Doe", "Engineering", "$75,000")
288
table.add_row("002", "Jane Smith", "Marketing", "$65,000")
289
table.add_row("003", "Bob Johnson", "Sales", "$55,000")
290
291
console.print(table)
292
293
# Table with column configuration
294
table = Table(show_header=True, header_style="bold red")
295
296
# Add columns with specific formatting
297
table.add_column(
298
"Product",
299
header_style="bold blue",
300
style="cyan",
301
min_width=10,
302
max_width=20
303
)
304
table.add_column(
305
"Price",
306
justify="right",
307
style="green",
308
header_style="bold green"
309
)
310
table.add_column(
311
"Status",
312
justify="center",
313
style="yellow",
314
vertical="middle"
315
)
316
317
table.add_row("Laptop", "$999.99", "In Stock")
318
table.add_row("Mouse", "$29.99", "Out of Stock")
319
table.add_row("Keyboard", "$79.99", "In Stock")
320
321
console.print(table)
322
323
# Grid layout (no borders)
324
grid = Table.grid(padding=1)
325
grid.add_column(style="red", justify="right")
326
grid.add_column(style="blue")
327
grid.add_column(style="green")
328
329
grid.add_row("Label 1:", "Value A", "Extra Info")
330
grid.add_row("Label 2:", "Value B", "More Info")
331
grid.add_row("Label 3:", "Value C", "Additional")
332
333
console.print(grid)
334
335
# Table with sections
336
table = Table()
337
table.add_column("Category")
338
table.add_column("Item")
339
table.add_column("Count")
340
341
table.add_row("Fruits", "Apples", "10")
342
table.add_row("Fruits", "Oranges", "15")
343
table.add_section() # Add section break
344
345
table.add_row("Vegetables", "Carrots", "8")
346
table.add_row("Vegetables", "Lettuce", "12")
347
348
console.print(table)
349
350
# Responsive table with ratios
351
table = Table(expand=True)
352
table.add_column("Description", ratio=3) # 3/4 of available width
353
table.add_column("Value", ratio=1, justify="right") # 1/4 of width
354
355
table.add_row("Very long description that will wrap", "100")
356
table.add_row("Short desc", "200")
357
358
console.print(table)
359
360
# Table with rich content
361
from rich.text import Text
362
from rich.panel import Panel
363
364
table = Table()
365
table.add_column("Rich Content")
366
table.add_column("Status")
367
368
# Add styled text
369
styled_text = Text("Important Item", style="bold red")
370
table.add_row(styled_text, "Active")
371
372
# Add panel as cell content
373
panel = Panel("Wrapped Content", style="blue")
374
table.add_row(panel, "Pending")
375
376
console.print(table)
377
```