| 1 |
Padding |
| 2 |
======= |
| 3 |
|
| 4 |
The :class:`~rich.padding.Padding` class may be used to add whitespace around text or other renderable. The following example will print the word "Hello" with a padding of 1 character, so there will be a blank line above and below, and a space on the left and right edges:: |
| 5 |
|
| 6 |
from rich import print |
| 7 |
from rich.padding import Padding |
| 8 |
test = Padding("Hello", 1) |
| 9 |
print(test) |
| 10 |
|
| 11 |
You can specify the padding on a more granular level by using a tuple of values rather than a single value. A tuple of 2 values sets the top/bottom and left/right padding, whereas a tuple of 4 values sets the padding for top, right, bottom, and left sides. You may recognize this scheme if you are familiar with CSS. |
| 12 |
|
| 13 |
For example, the following displays 2 blank lines above and below the text, and a padding of 4 spaces on the left and right sides:: |
| 14 |
|
| 15 |
from rich import print |
| 16 |
from rich.padding import Padding |
| 17 |
test = Padding("Hello", (2, 4)) |
| 18 |
print(test) |
| 19 |
|
| 20 |
The Padding class can also accept a ``style`` argument which applies a style to the padding and contents, and an ``expand`` switch which can be set to False to prevent the padding from extending to the full width of the terminal. Here's an example which demonstrates both these arguments:: |
| 21 |
|
| 22 |
from rich import print |
| 23 |
from rich.padding import Padding |
| 24 |
test = Padding("Hello", (2, 4), style="on blue", expand=False) |
| 25 |
print(test) |
| 26 |
|
| 27 |
Note that, as with all Rich renderables, you can use Padding in any context. For instance, if you want to emphasize an item in a :class:`~rich.table.Table` you could add a Padding object to a row with a padding of 1 and a style of "on red". |