Skip to content

Instantly share code, notes, and snippets.

@nathanrpage97
Created October 13, 2020 15:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanrpage97/6eb6b92cc55a7536ad344709bfabe4e9 to your computer and use it in GitHub Desktop.
Save nathanrpage97/6eb6b92cc55a7536ad344709bfabe4e9 to your computer and use it in GitHub Desktop.
Showcase of the Live feature branch
import inspect
import random
import time
from typing import Dict, List, Tuple
from rich.console import Console, RenderGroup
from rich.live import Live
from rich.panel import Panel
from rich.progress import Progress
from rich.syntax import Syntax
from rich.table import Table
from rich.text import Text
console = Console()
def table_example() -> None:
Data = List[List[int]]
def generate_table(data: Data) -> Table:
table = Table()
for data_row in data:
table.add_row(*[hex(data_cell) for data_cell in data_row])
return table
def generate_data() -> Data:
return [
[random.randint(0, 20) for _ in range(random.randint(0, 8))]
for _ in range(random.randint(0, 20))
]
with Live(console=console) as live_table:
for _ in range(20):
data = generate_data()
time.sleep(0.2)
console.print("hello")
live_table.update(generate_table(data))
def panel_example() -> None:
with Live() as live_panel:
for index in range(20):
panel = Panel(
f"Hello, [red]World! {index}" * (index ** 3 % 40), title="Welcome"
)
live_panel.update(panel)
time.sleep(0.2)
def table_example2() -> None:
exchanges = [
"SGD",
"MYR",
"EUR",
"USD",
"AUD",
"JPY",
"CNH",
"HKD",
"CAD",
"INR",
"DKK",
"GBP",
"RUB",
"NZD",
"MXN",
"IDR",
"TWD",
"THB",
"VND",
]
with Live(console=console) as live_table:
exchange_rate_dict: Dict[Tuple[str, str], float] = {}
for index in range(100):
select_exchange = exchanges[index % len(exchanges)]
time.sleep(0.1)
console.log("can still log")
for exchange in exchanges:
if exchange == select_exchange:
continue
exchange_rate_dict[(select_exchange, exchange)] = 20 / (
(random.random() * 40) + 1
)
if len(exchange_rate_dict) > len(exchanges) - 1:
exchange_rate_dict.pop(list(exchange_rate_dict.keys())[0])
table = Table(title="Exchange Rates")
table.add_column("Source Currency")
table.add_column("Destination Currency")
table.add_column("Exchange Rate")
for ((soure, dest), exchange_rate) in exchange_rate_dict.items():
table.add_row(
soure,
dest,
Text(
repr(exchange_rate),
style="red" if exchange_rate < 1.0 else "green",
),
)
live_table.update(table)
def render_group_example():
exchanges = [
"SGD",
"MYR",
"EUR",
"USD",
"AUD",
"JPY",
"CNH",
"HKD",
"CAD",
"INR",
"DKK",
"GBP",
"RUB",
"NZD",
"MXN",
"IDR",
"TWD",
"THB",
"VND",
]
with Live(console=console) as live_table:
exchange_rate_dict: Dict[Tuple[str, str], float] = {}
for index in range(100):
select_exchange = exchanges[index % len(exchanges)]
time.sleep(0.1)
console.log("can still log")
for exchange in exchanges:
if exchange == select_exchange:
continue
exchange_rate_dict[(select_exchange, exchange)] = 20 / (
(random.random() * 40) + 1
)
if len(exchange_rate_dict) > len(exchanges) - 1:
exchange_rate_dict.pop(list(exchange_rate_dict.keys())[0])
table = Table(title="Exchange Rates")
table.add_column("Source Currency")
table.add_column("Destination Currency")
table.add_column("Exchange Rate")
panel = Panel("Here is something cool", width=20)
for ((soure, dest), exchange_rate) in exchange_rate_dict.items():
table.add_row(
soure,
dest,
Text(
repr(exchange_rate),
style="red" if exchange_rate < 1.0 else "green",
),
)
live_table.update(RenderGroup(panel, table))
def render_table_squared_example():
exchanges = [
"SGD",
"MYR",
"EUR",
"USD",
"AUD",
"JPY",
"CNH",
"HKD",
"CAD",
"INR",
"DKK",
"GBP",
"RUB",
"NZD",
"MXN",
"IDR",
"TWD",
"THB",
"VND",
]
source_syntax = Syntax(
inspect.getsource(panel_example), "python", word_wrap=True, line_numbers=True
)
with Live(console=console) as live_table, Progress(auto_refresh=False) as progress:
exchange_rate_dict: Dict[Tuple[str, str], float] = {}
task = progress.add_task("iterate through")
index = 0
while not progress.finished:
index += 10
progress.update(task, completed=index)
select_exchange = exchanges[index % len(exchanges)]
time.sleep(0.1)
for exchange in exchanges:
if exchange == select_exchange:
continue
exchange_rate_dict[(select_exchange, exchange)] = 20 / (
(random.random() * 40) + 1
)
if len(exchange_rate_dict) > len(exchanges) - 1:
exchange_rate_dict.pop(list(exchange_rate_dict.keys())[0])
table = Table(title="Exchange Rates")
table.add_column("Source Currency")
table.add_column("Destination Currency")
table.add_column("Exchange Rate")
panel = Panel(
RenderGroup(f"Ran this {index} times", progress.get_renderable()),
width=60,
)
for ((soure, dest), exchange_rate) in exchange_rate_dict.items():
table.add_row(
soure,
dest,
Text(
repr(exchange_rate),
style="red" if exchange_rate < 1.0 else "green",
),
)
table_grid = Table.grid(padding=(1, 1), expand=True)
table_grid.add_column(width=80)
table_grid.add_column(ratio=1)
table_grid.add_row(
RenderGroup(panel, source_syntax), table,
)
live_table.update(table_grid)
@kasunsewwandika
Copy link

Thanks

@yxlwfds
Copy link

yxlwfds commented Jun 22, 2021

Tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment