0 dní v řadě · 0 bodů

Tahák · Python · Užitečné knihovny

rich a tqdm: hezký výstup a průběh

Vypíšeš tabulky, stromy a zvýrazněná hlášení přes rich a k dlouhým cyklům přidáš ukazatel průběhu z tqdm.

Otevřít celou lekciVšechny taháky
python
# python -m pip install rich tqdm
from rich.console import Console
from rich.markup import escape
from rich.table import Table
from rich.tree import Tree
from rich.panel import Panel
from tqdm import tqdm

console = Console()                       # stderr=True for messages
console.print("[bold red]Chyba[/]", escape(name))
console.print(text, markup=False, soft_wrap=True)
console.rule("Nadpis")
console.print(Panel("text", title="Titulek"))

t = Table(title="Název")
t.add_column("A"); t.add_column("B", justify="right")
t.add_row("x", str(1))                    # cells must be text
console.print(t)

tree = Tree("root"); branch = tree.add("a"); branch.add("b")

for x in tqdm(items, desc="Práce", unit="ks"): ...
for x in tqdm(generator(), total=n): ...  # generators need total
with tqdm(total=size, unit="B", unit_scale=True) as bar:
    bar.update(len(chunk))
tqdm.write("zpráva")                      # instead of print in a loop
tqdm(items, disable=None)                 # off when not a terminal