| 1 |
""" |
| 2 |
|
| 3 |
Demonstrates a dynamic Layout |
| 4 |
|
| 5 |
""" |
| 6 |
|
| 7 |
from datetime import datetime |
| 8 |
|
| 9 |
from time import sleep |
| 10 |
|
| 11 |
from rich.align import Align |
| 12 |
from rich.console import Console |
| 13 |
from rich.layout import Layout |
| 14 |
from rich.live import Live |
| 15 |
from rich.text import Text |
| 16 |
|
| 17 |
console = Console() |
| 18 |
layout = Layout() |
| 19 |
|
| 20 |
layout.split( |
| 21 |
Layout(name="header", size=1), |
| 22 |
Layout(ratio=1, name="main"), |
| 23 |
Layout(size=10, name="footer"), |
| 24 |
) |
| 25 |
|
| 26 |
layout["main"].split_row(Layout(name="side"), Layout(name="body", ratio=2)) |
| 27 |
|
| 28 |
layout["side"].split(Layout(), Layout()) |
| 29 |
|
| 30 |
layout["body"].update( |
| 31 |
Align.center( |
| 32 |
Text( |
| 33 |
"""This is a demonstration of rich.Layout\n\nHit Ctrl+C to exit""", |
| 34 |
justify="center", |
| 35 |
), |
| 36 |
vertical="middle", |
| 37 |
) |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
class Clock: |
| 42 |
"""Renders the time in the center of the screen.""" |
| 43 |
|
| 44 |
def __rich__(self) -> Text: |
| 45 |
return Text(datetime.now().ctime(), style="bold magenta", justify="center") |
| 46 |
|
| 47 |
|
| 48 |
layout["header"].update(Clock()) |
| 49 |
|
| 50 |
with Live(layout, screen=True, redirect_stderr=False) as live: |
| 51 |
try: |
| 52 |
while True: |
| 53 |
sleep(1) |
| 54 |
except KeyboardInterrupt: |
| 55 |
pass |