Debian Codegraph @

examples/rich_tables.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """An example of using Rich Tables within a cmd2 application for displaying tabular data.
3
4 While you can use any Python library for displaying tabular data within a cmd2 application,
5 we recommend using rich since that is built into cmd2.
6
7 Data comes from World Population Review: https://worldpopulationreview.com/
8 and https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)
9
10 NOTE: The flag emojis used require appropriate font support in your terminal
11 and/or IDE. Any "Nerd Font" should work. Some good options include:
12 - JetBrains Mono
13 - Fira Code
14 - Hack
15 - Monaspace
16 """
17
18 from rich.table import Table
19
20 import cmd2
21 from cmd2.colors import Color
22
23 CITY_HEADERS = ["Flag", "City", "Country", "2025 Population"]
24 CITY_DATA = [
25 ["๐Ÿ‡ฏ๐Ÿ‡ต", "Tokyo (ๆฑไบฌ)", "Japan", 37_036_200],
26 ["๐Ÿ‡ฎ๐Ÿ‡ณ", "Delhi", "India", 34_665_600],
27 ["๐Ÿ‡จ๐Ÿ‡ณ", "Shanghai (ไธŠๆตท)", "China", 30_482_100],
28 ["๐Ÿ‡ง๐Ÿ‡ฉ", "Dhaka", "Bangladesh", 24_652_900],
29 ["๐Ÿ‡ช๐Ÿ‡ฌ", "Cairo (ุงู„ู‚ุงู‡ุฑุฉ)", "Egypt", 23_074_200],
30 ["๐Ÿ‡ช๐Ÿ‡ฌ", "Sรฃo Paulo", "Brazil", 22_990_000],
31 ["๐Ÿ‡ฒ๐Ÿ‡ฝ", "Mexico City", "Mexico", 22_752_400],
32 ["๐Ÿ‡จ๐Ÿ‡ณ", "Beijing (ๅŒ—ไบฌ)", "China", 22_596_500],
33 ["๐Ÿ‡ฎ๐Ÿ‡ณ", "Mumbai", "India", 22_089_000],
34 ["๐Ÿ‡ฏ๐Ÿ‡ต", "Osaka (ๅคง้˜ช)", "Japan", 18_921_600],
35 ]
36 CITY_TITLE = "10 Largest Cities by Population 2025"
37 CITY_CAPTION = "Data from https://worldpopulationreview.com/"
38
39 COUNTRY_HEADERS = [
40 "Flag",
41 "Country",
42 "2025 Population",
43 "Area (M km^2)",
44 "Population Density (/km^2)",
45 "GDP (million US$)",
46 "GDP per capita (US$)",
47 ]
48 COUNTRY_DATA = [
49 ["๐Ÿ‡ฎ๐Ÿ‡ณ", "India", 1_463_870_000, 3.3, 492, 4_187_017, 2_878],
50 ["๐Ÿ‡จ๐Ÿ‡ณ", "China (ไธญๅ›ฝ)", 1_416_100_000, 9.7, 150, 19_231_705, 13_687],
51 ["๐Ÿ‡บ๐Ÿ‡ธ", "United States", 347_276_000, 9.4, 38, 30_507_217, 89_105],
52 ["๐Ÿ‡ฎ๐Ÿ‡ฉ", "Indonesia", 285_721_000, 1.9, 152, 1_429_743, 5_027],
53 ["๐Ÿ‡ต๐Ÿ‡ฐ", "Pakistan", 255_220_000, 0.9, 331, 373_072, 1_484],
54 ["๐Ÿ‡ณ๐Ÿ‡ฌ", "Nigeria", 237_528_000, 0.9, 261, 188_271, 807],
55 ["๐Ÿ‡ง๐Ÿ‡ท", "Brazil", 212_812_000, 8.5, 25, 2_125_958, 9_964],
56 ["๐Ÿ‡ง๐Ÿ‡ฉ", "Bangladesh", 175_687_000, 0.1, 1_350, 467_218, 2_689],
57 ["๐Ÿ‡ท๐Ÿ‡บ", "Russia (ั€ะพััะธั)", 143_997_000, 17.1, 9, 2_076_396, 14_258],
58 ["๐Ÿ‡ช๐Ÿ‡น", "Ethiopia (แŠฅแ‰ตแ‹ฎแŒตแ‹ซ)", 135_472_000, 1.1, 120, 117_457, 1_066],
59 ]
60 COUNTRY_TITLE = "10 Largest Countries by Population 2025"
61 COUNTRY_CAPTION = "Data from https://worldpopulationreview.com/ and Wikipedia"
62
63
64 class TableApp(cmd2.Cmd):
65 """Cmd2 application to demonstrate displaying tabular data using rich."""
66
67 DEFAULT_CATEGORY = "Table Commands"
68
69 def __init__(self) -> None:
70 """Initialize the cmd2 application."""
71 super().__init__()
72
73 # Prints an intro banner once upon application startup
74 self.intro = "Are you curious which countries and cities on Earth have the largest populations?"
75
76 def do_cities(self, _: cmd2.Statement) -> None:
77 """Display the cities with the largest population."""
78 table = Table(title=CITY_TITLE, caption=CITY_CAPTION)
79
80 for header in CITY_HEADERS:
81 table.add_column(header)
82
83 for row in CITY_DATA:
84 # Convert integers or floats to strings, since rich tables can not render int/float
85 str_row = [f"{item:,}" if isinstance(item, int) else str(item) for item in row]
86 table.add_row(*str_row)
87
88 self.poutput(table)
89
90 def do_countries(self, _: cmd2.Statement) -> None:
91 """Display the countries with the largest population."""
92 table = Table(title=COUNTRY_TITLE, caption=COUNTRY_CAPTION)
93
94 for header in COUNTRY_HEADERS:
95 justify = "right"
96 header_style = None
97 style = None
98 match header:
99 case population if "2025 Population" in population:
100 header_style = Color.BRIGHT_BLUE
101 style = Color.BLUE
102 case density if "Density" in density:
103 header_style = Color.BRIGHT_RED
104 style = Color.RED
105 case percap if "per capita" in percap:
106 header_style = Color.BRIGHT_GREEN
107 style = Color.GREEN
108 case flag if "Flag" in flag:
109 justify = "center"
110 case country if "Country" in country:
111 justify = "left"
112
113 table.add_column(header, justify=justify, header_style=header_style, style=style)
114
115 for row in COUNTRY_DATA:
116 # Convert integers or floats to strings, since rich tables can not render int/float
117 str_row = [f"{item:,}" if isinstance(item, int) else str(item) for item in row]
118 table.add_row(*str_row)
119
120 self.poutput(table)
121
122
123 if __name__ == "__main__":
124 app = TableApp()
125 app.cmdloop()