Debian Codegraph @

examples/table_movie.py

raw
1 """Same as the table_movie.py but uses Live to update"""
2 import time
3 from contextlib import contextmanager
4
5 from rich import box
6 from rich.align import Align
7 from rich.console import Console
8 from rich.live import Live
9 from rich.table import Table
10 from rich.text import Text
11
12 TABLE_DATA = [
13 [
14 "May 25, 1977",
15 "Star Wars Ep. [b]IV[/]: [i]A New Hope",
16 "$11,000,000",
17 "$1,554,475",
18 "$775,398,007",
19 ],
20 [
21 "May 21, 1980",
22 "Star Wars Ep. [b]V[/]: [i]The Empire Strikes Back",
23 "$23,000,000",
24 "$4,910,483",
25 "$547,969,004",
26 ],
27 [
28 "May 25, 1983",
29 "Star Wars Ep. [b]VI[/b]: [i]Return of the Jedi",
30 "$32,500,000",
31 "$23,019,618",
32 "$475,106,177",
33 ],
34 [
35 "May 19, 1999",
36 "Star Wars Ep. [b]I[/b]: [i]The phantom Menace",
37 "$115,000,000",
38 "$64,810,870",
39 "$1,027,044,677",
40 ],
41 [
42 "May 16, 2002",
43 "Star Wars Ep. [b]II[/b]: [i]Attack of the Clones",
44 "$115,000,000",
45 "$80,027,814",
46 "$656,695,615",
47 ],
48 [
49 "May 19, 2005",
50 "Star Wars Ep. [b]III[/b]: [i]Revenge of the Sith",
51 "$115,500,000",
52 "$380,270,577",
53 "$848,998,877",
54 ],
55 ]
56
57 console = Console()
58
59 BEAT_TIME = 0.04
60
61
62 @contextmanager
63 def beat(length: int = 1) -> None:
64 yield
65 time.sleep(length * BEAT_TIME)
66
67
68 table = Table(show_footer=False)
69 table_centered = Align.center(table)
70
71 console.clear()
72
73 with Live(table_centered, console=console, screen=False, refresh_per_second=20):
74 with beat(10):
75 table.add_column("Release Date", no_wrap=True)
76
77 with beat(10):
78 table.add_column("Title", Text.from_markup("[b]Total", justify="right"))
79
80 with beat(10):
81 table.add_column("Budget", "[u]$412,000,000", no_wrap=True)
82
83 with beat(10):
84 table.add_column("Opening Weekend", "[u]$577,703,455", no_wrap=True)
85
86 with beat(10):
87 table.add_column("Box Office", "[u]$4,331,212,357", no_wrap=True)
88
89 with beat(10):
90 table.title = "Star Wars Box Office"
91
92 with beat(10):
93 table.title = (
94 "[not italic]:popcorn:[/] Star Wars Box Office [not italic]:popcorn:[/]"
95 )
96
97 with beat(10):
98 table.caption = "Made with Rich"
99
100 with beat(10):
101 table.caption = "Made with [b]Rich[/b]"
102
103 with beat(10):
104 table.caption = "Made with [b magenta not dim]Rich[/]"
105
106 for row in TABLE_DATA:
107 with beat(10):
108 table.add_row(*row)
109
110 with beat(10):
111 table.show_footer = True
112
113 table_width = console.measure(table).maximum
114
115 with beat(10):
116 table.columns[2].justify = "right"
117
118 with beat(10):
119 table.columns[3].justify = "right"
120
121 with beat(10):
122 table.columns[4].justify = "right"
123
124 with beat(10):
125 table.columns[2].header_style = "bold red"
126
127 with beat(10):
128 table.columns[3].header_style = "bold green"
129
130 with beat(10):
131 table.columns[4].header_style = "bold blue"
132
133 with beat(10):
134 table.columns[2].style = "red"
135
136 with beat(10):
137 table.columns[3].style = "green"
138
139 with beat(10):
140 table.columns[4].style = "blue"
141
142 with beat(10):
143 table.columns[0].style = "cyan"
144 table.columns[0].header_style = "bold cyan"
145
146 with beat(10):
147 table.columns[1].style = "magenta"
148 table.columns[1].header_style = "bold magenta"
149
150 with beat(10):
151 table.columns[2].footer_style = "bright_red"
152
153 with beat(10):
154 table.columns[3].footer_style = "bright_green"
155
156 with beat(10):
157 table.columns[4].footer_style = "bright_blue"
158
159 with beat(10):
160 table.row_styles = ["none", "dim"]
161
162 with beat(10):
163 table.border_style = "bright_yellow"
164
165 for box_style in [
166 box.SQUARE,
167 box.MINIMAL,
168 box.SIMPLE,
169 box.SIMPLE_HEAD,
170 ]:
171 with beat(10):
172 table.box = box_style
173
174 with beat(10):
175 table.pad_edge = False
176
177 original_width = console.measure(table).maximum
178
179 for width in range(original_width, console.width, 2):
180 with beat(1):
181 table.width = width
182
183 for width in range(console.width, original_width, -2):
184 with beat(1):
185 table.width = width
186
187 for width in range(original_width, 90, -2):
188 with beat(1):
189 table.width = width
190
191 for width in range(90, original_width + 1, 2):
192 with beat(1):
193 table.width = width
194
195 with beat(2):
196 table.width = None