mirror of https://github.com/Textualize/rich.git
simplify live demo example
This commit is contained in:
parent
4ac0cb8990
commit
e0337f5060
|
@ -101,7 +101,6 @@ You should set this to something lower than 4 if you know your updates will not
|
||||||
|
|
||||||
You might want to disable auto-refresh entirely if your updates are not very frequent, which you can do by setting ``auto_refresh=False`` on the constructor.
|
You might want to disable auto-refresh entirely if your updates are not very frequent, which you can do by setting ``auto_refresh=False`` on the constructor.
|
||||||
If you disable auto-refresh you will need to call :meth:`~rich.live.Live.refresh` manually or :meth:`~rich.live.Live.update` with ``refresh=True``.
|
If you disable auto-refresh you will need to call :meth:`~rich.live.Live.refresh` manually or :meth:`~rich.live.Live.update` with ``refresh=True``.
|
||||||
This is
|
|
||||||
|
|
||||||
Hide "Terminal Too Small" Warning
|
Hide "Terminal Too Small" Warning
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
164
rich/live.py
164
rich/live.py
|
@ -243,103 +243,105 @@ class Live(JupyterMixin, RenderHook):
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
from itertools import cycle
|
||||||
from typing import Dict, List, Tuple
|
from typing import Dict, List, Tuple
|
||||||
|
|
||||||
from .console import Console, RenderGroup
|
from .console import Console
|
||||||
from .live import Live
|
from .live import Live
|
||||||
from .panel import Panel
|
from .panel import Panel
|
||||||
|
from .rule import Rule
|
||||||
|
from .syntax import Syntax
|
||||||
from .table import Table
|
from .table import Table
|
||||||
from .text import Text
|
from .text import Text
|
||||||
|
|
||||||
console = Console()
|
console = Console()
|
||||||
|
|
||||||
def table_example() -> None:
|
syntax = Syntax(
|
||||||
Data = List[List[int]]
|
'''def loop_last(values: Iterable[T]) -> Iterable[Tuple[bool, T]]:
|
||||||
|
"""Iterate and generate a tuple with a flag for last value."""
|
||||||
|
iter_values = iter(values)
|
||||||
|
try:
|
||||||
|
previous_value = next(iter_values)
|
||||||
|
except StopIteration:
|
||||||
|
return
|
||||||
|
for value in iter_values:
|
||||||
|
yield False, previous_value
|
||||||
|
previous_value = value
|
||||||
|
yield True, previous_value''',
|
||||||
|
"python",
|
||||||
|
line_numbers=True,
|
||||||
|
)
|
||||||
|
|
||||||
def generate_table(data: Data) -> Table:
|
table = Table("foo", "bar", "baz")
|
||||||
table = Table()
|
table.add_row("1", "2", "3")
|
||||||
for data_row in data:
|
|
||||||
table.add_row(*[hex(data_cell) for data_cell in data_row])
|
|
||||||
|
|
||||||
return table
|
progress_renderables = [
|
||||||
|
"You can make the terminal shorter and taller to see the live table hide"
|
||||||
|
"Text may be printed while the progress bars are rendering.",
|
||||||
|
Panel("In fact, [i]any[/i] renderable will work"),
|
||||||
|
"Such as [magenta]tables[/]...",
|
||||||
|
table,
|
||||||
|
"Pretty printed structures...",
|
||||||
|
{"type": "example", "text": "Pretty printed"},
|
||||||
|
"Syntax...",
|
||||||
|
syntax,
|
||||||
|
Rule("Give it a try!"),
|
||||||
|
]
|
||||||
|
|
||||||
def generate_data() -> Data:
|
examples = cycle(progress_renderables)
|
||||||
return [
|
|
||||||
[random.randint(0, 20) for _ in range(random.randint(0, 8))]
|
|
||||||
for _ in range(random.randint(12, 20))
|
|
||||||
]
|
|
||||||
|
|
||||||
with Live(console=console, refresh_per_second=1, transient=True) as live_table:
|
exchanges = [
|
||||||
for _ in range(20):
|
"SGD",
|
||||||
data = generate_data()
|
"MYR",
|
||||||
time.sleep(0.5)
|
"EUR",
|
||||||
console.print("hello")
|
"USD",
|
||||||
live_table.update(generate_table(data))
|
"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] = {}
|
||||||
|
|
||||||
def panel_example() -> None:
|
for index in range(100):
|
||||||
|
select_exchange = exchanges[index % len(exchanges)]
|
||||||
|
|
||||||
with Live(auto_refresh=False) as live_panel:
|
for exchange in exchanges:
|
||||||
for index in range(20):
|
if exchange == select_exchange:
|
||||||
panel = Panel(f"Hello, [red]World! {index}\n" * index, title="Welcome")
|
continue
|
||||||
live_panel.update(panel, refresh=True)
|
time.sleep(0.4)
|
||||||
time.sleep(0.2)
|
if random.randint(0, 10) < 1:
|
||||||
|
console.log(next(examples))
|
||||||
|
exchange_rate_dict[(select_exchange, exchange)] = 200 / (
|
||||||
|
(random.random() * 320) + 1
|
||||||
|
)
|
||||||
|
if len(exchange_rate_dict) > len(exchanges) - 1:
|
||||||
|
exchange_rate_dict.pop(list(exchange_rate_dict.keys())[0])
|
||||||
|
table = Table(title="Exchange Rates")
|
||||||
|
|
||||||
def table_example2() -> None:
|
table.add_column("Source Currency")
|
||||||
exchanges = [
|
table.add_column("Destination Currency")
|
||||||
"SGD",
|
table.add_column("Exchange Rate")
|
||||||
"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):
|
for ((soure, dest), exchange_rate) in exchange_rate_dict.items():
|
||||||
select_exchange = exchanges[index % len(exchanges)]
|
table.add_row(
|
||||||
time.sleep(0.1)
|
soure,
|
||||||
console.log("can still log")
|
dest,
|
||||||
for exchange in exchanges:
|
Text(
|
||||||
if exchange == select_exchange:
|
f"{exchange_rate:.4f}",
|
||||||
continue
|
style="red" if exchange_rate < 1.0 else "green",
|
||||||
|
),
|
||||||
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")
|
live_table.update(table)
|
||||||
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(
|
|
||||||
f"{exchange_rate:.4f}",
|
|
||||||
style="red" if exchange_rate < 1.0 else "green",
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
live_table.update(table)
|
|
||||||
|
|
||||||
table_example()
|
|
||||||
table_example2()
|
|
||||||
panel_example()
|
|
Loading…
Reference in New Issue