consolidate table_movie_live and table_movie into one file and update refs

This commit is contained in:
Nathan Page 2020-12-02 23:41:52 -08:00
parent 068f4328f2
commit 7a82414c62
3 changed files with 50 additions and 285 deletions

View file

@ -167,6 +167,6 @@ This feature is enabled by default, but you can disable by setting ``redirect_st
Examples
--------
See `table_movie_live.py <https://github.com/willmcgugan/rich/blob/master/examples/table_movie_live.py>`_ and
See `table_movie.py <https://github.com/willmcgugan/rich/blob/master/examples/table_movie.py>`_ and
`top_lite_simulator.py <https://github.com/willmcgugan/rich/blob/master/examples/top_lite_simulator.py>`_
for deeper examples of live displaying.

View file

@ -1,12 +1,16 @@
"""Same as the table_movie.py but uses Live to update"""
from contextlib import contextmanager
import time
from rich.console import Console
from rich.columns import Columns
from rich.table import Table
from rich.measure import Measurement
from rich import box
from rich.text import Text
from rich.live import Live
TABLE_DATA = [
[
"May 25, 1977",
@ -60,136 +64,110 @@ BEAT_TIME = 0.04
@contextmanager
def beat(length: int = 1) -> None:
with console:
console.clear()
yield
time.sleep(length * BEAT_TIME)
table = Table(show_footer=False)
table_centered = Columns((table,), align="center", expand=True)
console.clear()
console.show_cursor(False)
try:
table.add_column("Release Date", no_wrap=True)
with beat(10):
console.print(table, justify="center")
with Live(
table_centered, console=console, refresh_per_second=10, vertical_overflow="ellipsis"
):
table.add_column("Title", Text.from_markup("[b]Total", justify="right"))
with beat(10):
console.print(table, justify="center")
table.add_column("Release Date", no_wrap=True)
table.add_column("Budget", "[u]$412,000,000", no_wrap=True)
with beat(10):
console.print(table, justify="center")
table.add_column("Title", Text.from_markup("[b]Total", justify="right"))
table.add_column("Opening Weekend", "[u]$577,703,455", no_wrap=True)
with beat(10):
console.print(table, justify="center")
table.add_column("Budget", "[u]$412,000,000", no_wrap=True)
table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True)
with beat(10):
console.print(table, justify="center")
table.add_column("Opening Weekend", "[u]$577,703,455", no_wrap=True)
table.title = "Star Wars Box Office"
with beat(10):
console.print(table, justify="center")
table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True)
table.title = (
"[not italic]:popcorn:[/] Star Wars Box Office [not italic]:popcorn:[/]"
)
with beat(10):
console.print(table, justify="center")
table.title = "Star Wars Box Office"
table.caption = "Made with Rich"
with beat(10):
console.print(table, justify="center")
table.title = (
"[not italic]:popcorn:[/] Star Wars Box Office [not italic]:popcorn:[/]"
)
table.caption = "Made with [b]Rich[/b]"
with beat(10):
console.print(table, justify="center")
table.caption = "Made with Rich"
table.caption = "Made with [b magenta not dim]Rich[/]"
with beat(10):
console.print(table, justify="center")
table.caption = "Made with [b]Rich[/b]"
with beat(10):
table.caption = "Made with [b magenta not dim]Rich[/]"
for row in TABLE_DATA:
table.add_row(*row)
with beat(10):
console.print(table, justify="center")
table.add_row(*row)
table.show_footer = True
with beat(10):
console.print(table, justify="center")
table.show_footer = True
table_width = Measurement.get(console, table, console.width).maximum
table.columns[2].justify = "right"
with beat(10):
console.print(table, justify="center")
table.columns[2].justify = "right"
table.columns[3].justify = "right"
with beat(10):
console.print(table, justify="center")
table.columns[3].justify = "right"
table.columns[4].justify = "right"
with beat(10):
console.print(table, justify="center")
table.columns[4].justify = "right"
table.columns[2].header_style = "bold red"
with beat(10):
console.print(table, justify="center")
table.columns[2].header_style = "bold red"
table.columns[3].header_style = "bold green"
with beat(10):
console.print(table, justify="center")
table.columns[3].header_style = "bold green"
table.columns[4].header_style = "bold blue"
with beat(10):
console.print(table, justify="center")
table.columns[4].header_style = "bold blue"
table.columns[2].style = "red"
with beat(10):
console.print(table, justify="center")
table.columns[2].style = "red"
table.columns[3].style = "green"
with beat(10):
console.print(table, justify="center")
table.columns[3].style = "green"
table.columns[4].style = "blue"
with beat(10):
console.print(table, justify="center")
table.columns[4].style = "blue"
table.columns[0].style = "cyan"
table.columns[0].header_style = "bold cyan"
with beat(10):
console.print(table, justify="center")
table.columns[0].style = "cyan"
table.columns[0].header_style = "bold cyan"
table.columns[1].style = "magenta"
table.columns[1].header_style = "bold magenta"
with beat(10):
console.print(table, justify="center")
table.columns[1].style = "magenta"
table.columns[1].header_style = "bold magenta"
table.columns[2].footer_style = "bright_red"
with beat(10):
console.print(table, justify="center")
table.columns[2].footer_style = "bright_red"
table.columns[3].footer_style = "bright_green"
with beat(10):
console.print(table, justify="center")
table.columns[3].footer_style = "bright_green"
table.columns[4].footer_style = "bright_blue"
with beat(10):
console.print(table, justify="center")
table.columns[4].footer_style = "bright_blue"
table.row_styles = ["none", "dim"]
with beat(10):
console.print(table, justify="center")
table.row_styles = ["none", "dim"]
table.border_style = "bright_yellow"
with beat(10):
console.print(table, justify="center")
table.border_style = "bright_yellow"
for box in [
box.SQUARE,
@ -197,39 +175,29 @@ try:
box.SIMPLE,
box.SIMPLE_HEAD,
]:
table.box = box
with beat(10):
console.print(table, justify="center")
table.box = box
table.pad_edge = False
with beat(10):
console.print(table, justify="center")
table.pad_edge = False
original_width = Measurement.get(console, table).maximum
for width in range(original_width, console.width, 2):
table.width = width
with beat(2):
console.print(table, justify="center")
table.width = width
for width in range(console.width, original_width, -2):
table.width = width
with beat(2):
console.print(table, justify="center")
table.width = width
for width in range(original_width, 90, -2):
table.width = width
with beat(2):
console.print(table, justify="center")
table.width = width
for width in range(90, original_width + 1, 2):
table.width = width
with beat(2):
console.print(table, justify="center")
table.width = width
table.width = None
with beat(2):
console.print(table, justify="center")
finally:
console.show_cursor(True)
table.width = None

View file

@ -1,203 +0,0 @@
"""Same as the table_movie.py but uses Live to update"""
from contextlib import contextmanager
import time
from rich.console import Console
from rich.columns import Columns
from rich.table import Table
from rich.measure import Measurement
from rich import box
from rich.text import Text
from rich.live import Live
TABLE_DATA = [
[
"May 25, 1977",
"Star Wars Ep. [b]IV[/]: [i]A New Hope",
"$11,000,000",
"$1,554,475",
"$775,398,007",
],
[
"May 21, 1980",
"Star Wars Ep. [b]V[/]: [i]The Empire Strikes Back",
"$23,000,000",
"$4,910,483",
"$547,969,004",
],
[
"May 25, 1983",
"Star Wars Ep. [b]VI[/b]: [i]Return of the Jedi",
"$32,500,000",
"$23,019,618",
"$475,106,177",
],
[
"May 19, 1999",
"Star Wars Ep. [b]I[/b]: [i]The phantom Menace",
"$115,000,000",
"$64,810,870",
"$1,027,044,677",
],
[
"May 16, 2002",
"Star Wars Ep. [b]II[/b]: [i]Attack of the Clones",
"$115,000,000",
"$80,027,814",
"$656,695,615",
],
[
"May 19, 2005",
"Star Wars Ep. [b]III[/b]: [i]Revenge of the Sith",
"$115,500,000",
"$380,270,577",
"$848,998,877",
],
]
console = Console()
BEAT_TIME = 0.04
@contextmanager
def beat(length: int = 1) -> None:
with console:
yield
time.sleep(length * BEAT_TIME)
table = Table(show_footer=False)
table_centered = Columns((table,), align="center", expand=True)
console.clear()
with Live(
table_centered, console=console, refresh_per_second=10, vertical_overflow="ellipsis"
):
with beat(10):
table.add_column("Release Date", no_wrap=True)
with beat(10):
table.add_column("Title", Text.from_markup("[b]Total", justify="right"))
with beat(10):
table.add_column("Budget", "[u]$412,000,000", no_wrap=True)
with beat(10):
table.add_column("Opening Weekend", "[u]$577,703,455", no_wrap=True)
with beat(10):
table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True)
with beat(10):
table.title = "Star Wars Box Office"
with beat(10):
table.title = (
"[not italic]:popcorn:[/] Star Wars Box Office [not italic]:popcorn:[/]"
)
with beat(10):
table.caption = "Made with Rich"
with beat(10):
table.caption = "Made with [b]Rich[/b]"
with beat(10):
table.caption = "Made with [b magenta not dim]Rich[/]"
for row in TABLE_DATA:
with beat(10):
table.add_row(*row)
with beat(10):
table.show_footer = True
table_width = Measurement.get(console, table, console.width).maximum
with beat(10):
table.columns[2].justify = "right"
with beat(10):
table.columns[3].justify = "right"
with beat(10):
table.columns[4].justify = "right"
with beat(10):
table.columns[2].header_style = "bold red"
with beat(10):
table.columns[3].header_style = "bold green"
with beat(10):
table.columns[4].header_style = "bold blue"
with beat(10):
table.columns[2].style = "red"
with beat(10):
table.columns[3].style = "green"
with beat(10):
table.columns[4].style = "blue"
with beat(10):
table.columns[0].style = "cyan"
table.columns[0].header_style = "bold cyan"
with beat(10):
table.columns[1].style = "magenta"
table.columns[1].header_style = "bold magenta"
with beat(10):
table.columns[2].footer_style = "bright_red"
with beat(10):
table.columns[3].footer_style = "bright_green"
with beat(10):
table.columns[4].footer_style = "bright_blue"
with beat(10):
table.row_styles = ["none", "dim"]
with beat(10):
table.border_style = "bright_yellow"
for box in [
box.SQUARE,
box.MINIMAL,
box.SIMPLE,
box.SIMPLE_HEAD,
]:
with beat(10):
table.box = box
with beat(10):
table.pad_edge = False
original_width = Measurement.get(console, table).maximum
for width in range(original_width, console.width, 2):
with beat(2):
table.width = width
for width in range(console.width, original_width, -2):
with beat(2):
table.width = width
for width in range(original_width, 90, -2):
with beat(2):
table.width = width
for width in range(90, original_width + 1, 2):
with beat(2):
table.width = width
with beat(2):
table.width = None