rich/docs/source/group.rst

31 lines
1.3 KiB
ReStructuredText
Raw Normal View History

2020-09-12 13:31:04 +00:00
Render Groups
=============
2021-07-28 08:10:23 +00:00
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`.
2020-09-12 13:31:04 +00:00
2021-07-28 08:10:23 +00:00
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::
2020-09-12 13:31:04 +00:00
from rich import print
2021-08-03 12:25:18 +00:00
from rich.console import Group
2020-09-12 13:31:04 +00:00
from rich.panel import Panel
2021-07-28 08:10:23 +00:00
panel_group = Group(
2020-09-12 13:31:04 +00:00
Panel("Hello", style="on blue"),
Panel("World", style="on red"),
)
print(Panel(panel_group))
2021-07-28 08:10:23 +00:00
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::
2020-09-12 13:31:04 +00:00
from rich import print
2021-07-28 08:10:23 +00:00
from rich.console import group
2020-09-12 13:31:04 +00:00
from rich.panel import Panel
2021-07-28 08:10:23 +00:00
@group()
2020-09-12 13:31:04 +00:00
def get_panels():
yield Panel("Hello", style="on blue")
yield Panel("World", style="on red")
2021-06-11 08:13:09 +00:00
print(Panel(get_panels()))