| 1 |
""" |
| 2 |
Demonstrates a Rich "application" using the Layout and Live classes. |
| 3 |
|
| 4 |
""" |
| 5 |
|
| 6 |
from datetime import datetime |
| 7 |
|
| 8 |
from rich import box |
| 9 |
from rich.align import Align |
| 10 |
from rich.console import Console, Group |
| 11 |
from rich.layout import Layout |
| 12 |
from rich.panel import Panel |
| 13 |
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn |
| 14 |
from rich.syntax import Syntax |
| 15 |
from rich.table import Table |
| 16 |
|
| 17 |
console = Console() |
| 18 |
|
| 19 |
|
| 20 |
def make_layout() -> Layout: |
| 21 |
"""Define the layout.""" |
| 22 |
layout = Layout(name="root") |
| 23 |
|
| 24 |
layout.split( |
| 25 |
Layout(name="header", size=3), |
| 26 |
Layout(name="main", ratio=1), |
| 27 |
Layout(name="footer", size=7), |
| 28 |
) |
| 29 |
layout["main"].split_row( |
| 30 |
Layout(name="side"), |
| 31 |
Layout(name="body", ratio=2, minimum_size=60), |
| 32 |
) |
| 33 |
layout["side"].split(Layout(name="box1"), Layout(name="box2")) |
| 34 |
return layout |
| 35 |
|
| 36 |
|
| 37 |
def make_sponsor_message() -> Panel: |
| 38 |
"""Some example content.""" |
| 39 |
sponsor_message = Table.grid(padding=1) |
| 40 |
sponsor_message.add_column(style="green", justify="right") |
| 41 |
sponsor_message.add_column(no_wrap=True) |
| 42 |
sponsor_message.add_row( |
| 43 |
"Twitter", |
| 44 |
"[u blue link=https://twitter.com/textualize]https://twitter.com/textualize", |
| 45 |
) |
| 46 |
sponsor_message.add_row( |
| 47 |
"CEO", |
| 48 |
"[u blue link=https://twitter.com/willmcgugan]https://twitter.com/willmcgugan", |
| 49 |
) |
| 50 |
sponsor_message.add_row( |
| 51 |
"Textualize", "[u blue link=https://www.textualize.io]https://www.textualize.io" |
| 52 |
) |
| 53 |
|
| 54 |
message = Table.grid(padding=1) |
| 55 |
message.add_column() |
| 56 |
message.add_column(no_wrap=True) |
| 57 |
message.add_row(sponsor_message) |
| 58 |
|
| 59 |
message_panel = Panel( |
| 60 |
Align.center( |
| 61 |
Group("\n", Align.center(sponsor_message)), |
| 62 |
vertical="middle", |
| 63 |
), |
| 64 |
box=box.ROUNDED, |
| 65 |
padding=(1, 2), |
| 66 |
title="[b red]Thanks for trying out Rich!", |
| 67 |
border_style="bright_blue", |
| 68 |
) |
| 69 |
return message_panel |
| 70 |
|
| 71 |
|
| 72 |
class Header: |
| 73 |
"""Display header with clock.""" |
| 74 |
|
| 75 |
def __rich__(self) -> Panel: |
| 76 |
grid = Table.grid(expand=True) |
| 77 |
grid.add_column(justify="center", ratio=1) |
| 78 |
grid.add_column(justify="right") |
| 79 |
grid.add_row( |
| 80 |
"[b]Rich[/b] Layout application", |
| 81 |
datetime.now().ctime().replace(":", "[blink]:[/]"), |
| 82 |
) |
| 83 |
return Panel(grid, style="white on blue") |
| 84 |
|
| 85 |
|
| 86 |
def make_syntax() -> Syntax: |
| 87 |
code = """\ |
| 88 |
def ratio_resolve(total: int, edges: List[Edge]) -> List[int]: |
| 89 |
sizes = [(edge.size or None) for edge in edges] |
| 90 |
|
| 91 |
# While any edges haven't been calculated |
| 92 |
while any(size is None for size in sizes): |
| 93 |
# Get flexible edges and index to map these back on to sizes list |
| 94 |
flexible_edges = [ |
| 95 |
(index, edge) |
| 96 |
for index, (size, edge) in enumerate(zip(sizes, edges)) |
| 97 |
if size is None |
| 98 |
] |
| 99 |
# Remaining space in total |
| 100 |
remaining = total - sum(size or 0 for size in sizes) |
| 101 |
if remaining <= 0: |
| 102 |
# No room for flexible edges |
| 103 |
sizes[:] = [(size or 0) for size in sizes] |
| 104 |
break |
| 105 |
# Calculate number of characters in a ratio portion |
| 106 |
portion = remaining / sum((edge.ratio or 1) for _, edge in flexible_edges) |
| 107 |
|
| 108 |
# If any edges will be less than their minimum, replace size with the minimum |
| 109 |
for index, edge in flexible_edges: |
| 110 |
if portion * edge.ratio <= edge.minimum_size: |
| 111 |
sizes[index] = edge.minimum_size |
| 112 |
break |
| 113 |
else: |
| 114 |
# Distribute flexible space and compensate for rounding error |
| 115 |
# Since edge sizes can only be integers we need to add the remainder |
| 116 |
# to the following line |
| 117 |
_modf = modf |
| 118 |
remainder = 0.0 |
| 119 |
for index, edge in flexible_edges: |
| 120 |
remainder, size = _modf(portion * edge.ratio + remainder) |
| 121 |
sizes[index] = int(size) |
| 122 |
break |
| 123 |
# Sizes now contains integers only |
| 124 |
return cast(List[int], sizes) |
| 125 |
""" |
| 126 |
syntax = Syntax(code, "python", line_numbers=True) |
| 127 |
return syntax |
| 128 |
|
| 129 |
|
| 130 |
job_progress = Progress( |
| 131 |
"{task.description}", |
| 132 |
SpinnerColumn(), |
| 133 |
BarColumn(), |
| 134 |
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"), |
| 135 |
) |
| 136 |
job_progress.add_task("[green]Cooking") |
| 137 |
job_progress.add_task("[magenta]Baking", total=200) |
| 138 |
job_progress.add_task("[cyan]Mixing", total=400) |
| 139 |
|
| 140 |
total = sum(task.total for task in job_progress.tasks) |
| 141 |
overall_progress = Progress() |
| 142 |
overall_task = overall_progress.add_task("All Jobs", total=int(total)) |
| 143 |
|
| 144 |
progress_table = Table.grid(expand=True) |
| 145 |
progress_table.add_row( |
| 146 |
Panel( |
| 147 |
overall_progress, |
| 148 |
title="Overall Progress", |
| 149 |
border_style="green", |
| 150 |
padding=(2, 2), |
| 151 |
), |
| 152 |
Panel(job_progress, title="[b]Jobs", border_style="red", padding=(1, 2)), |
| 153 |
) |
| 154 |
|
| 155 |
|
| 156 |
layout = make_layout() |
| 157 |
layout["header"].update(Header()) |
| 158 |
layout["body"].update(make_sponsor_message()) |
| 159 |
layout["box2"].update(Panel(make_syntax(), border_style="green")) |
| 160 |
layout["box1"].update(Panel(layout.tree, border_style="red")) |
| 161 |
layout["footer"].update(progress_table) |
| 162 |
|
| 163 |
|
| 164 |
from time import sleep |
| 165 |
|
| 166 |
from rich.live import Live |
| 167 |
|
| 168 |
with Live(layout, refresh_per_second=10, screen=True): |
| 169 |
while not overall_progress.finished: |
| 170 |
sleep(0.1) |
| 171 |
for job in job_progress.tasks: |
| 172 |
if not job.finished: |
| 173 |
job_progress.advance(job.id) |
| 174 |
|
| 175 |
completed = sum(task.completed for task in job_progress.tasks) |
| 176 |
overall_progress.update(overall_task, completed=completed) |