| 1 |
Render Groups |
| 2 |
============= |
| 3 |
|
| 4 |
The :class:`~rich.console.Group` class allows you to group several renderables together so they may be rendered in a context where only a single renderable may be supplied. For instance, you might want to display several renderables within a :class:`~rich.panel.Panel`. |
| 5 |
|
| 6 |
To render two panels within a third panel, you would construct a Group with the *child* renderables as positional arguments then wrap the result in another Panel:: |
| 7 |
|
| 8 |
from rich import print |
| 9 |
from rich.console import Group |
| 10 |
from rich.panel import Panel |
| 11 |
|
| 12 |
panel_group = Group( |
| 13 |
Panel("Hello", style="on blue"), |
| 14 |
Panel("World", style="on red"), |
| 15 |
) |
| 16 |
print(Panel(panel_group)) |
| 17 |
|
| 18 |
|
| 19 |
This pattern is nice when you know in advance what renderables will be in a group, but can get awkward if you have a larger number of renderables, especially if they are dynamic. Rich provides a :func:`~rich.console.group` decorator to help with these situations. The decorator builds a group from an iterator of renderables. The following is the equivalent of the previous example using the decorator:: |
| 20 |
|
| 21 |
from rich import print |
| 22 |
from rich.console import group |
| 23 |
from rich.panel import Panel |
| 24 |
|
| 25 |
@group() |
| 26 |
def get_panels(): |
| 27 |
yield Panel("Hello", style="on blue") |
| 28 |
yield Panel("World", style="on red") |
| 29 |
|
| 30 |
print(Panel(get_panels())) |