Skip to content

Commit 2f55890

Browse files
authored
Rich table example (#1488)
Created simple example of using rich tables within a cmd2 application.
1 parent 91e5bc2 commit 2f55890

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed

examples/rich_tables.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
11+
from rich.table import Table
12+
13+
import cmd2
14+
from cmd2.colors import Color
15+
16+
CITY_HEADERS = ['Flag', 'City', 'Country', '2025 Population']
17+
CITY_DATA = [
18+
["🇯🇵", "Tokyo (東京)", "Japan", 37_036_200],
19+
["🇮🇳", "Delhi", "India", 34_665_600],
20+
["🇨🇳", "Shanghai (上海)", "China", 30_482_100],
21+
["🇧🇩", "Dhaka", "Bangladesh", 24_652_900],
22+
["🇪🇬", "Cairo (القاهرة)", "Egypt", 23_074_200],
23+
["🇪🇬", "São Paulo", "Brazil", 22_990_000],
24+
["🇲🇽", "Mexico City", "Mexico", 22_752_400],
25+
["🇨🇳", "Beijing (北京)", "China", 22_596_500],
26+
["🇮🇳", "Mumbai", "India", 22_089_000],
27+
["🇯🇵", "Osaka (大阪)", "Japan", 18_921_600],
28+
]
29+
CITY_TITLE = "10 Largest Cities by Population 2025"
30+
CITY_CAPTION = "Data from https://worldpopulationreview.com/"
31+
32+
COUNTRY_HEADERS = [
33+
'Flag',
34+
'Country',
35+
'2025 Population',
36+
'Area (M km^2)',
37+
'Population Density (/km^2)',
38+
'GDP (million US$)',
39+
'GDP per capita (US$)',
40+
]
41+
COUNTRY_DATA = [
42+
["🇮🇳", "India", 1_463_870_000, 3.3, 492, 4_187_017, 2_878],
43+
["🇨🇳", "China (中国)", 1_416_100_000, 9.7, 150, 19_231_705, 13_687],
44+
["🇺🇸", "United States", 347_276_000, 9.4, 38, 30_507_217, 89_105],
45+
["🇮🇩", "Indonesia", 285_721_000, 1.9, 152, 1_429_743, 5_027],
46+
["🇵🇰", "Pakistan", 255_220_000, 0.9, 331, 373_072, 1_484],
47+
["🇳🇬", "Nigeria", 237_528_000, 0.9, 261, 188_271, 807],
48+
["🇧🇷", "Brazil", 212_812_000, 8.5, 25, 2_125_958, 9_964],
49+
["🇧🇩", "Bangladesh", 175_687_000, 0.1, 1_350, 467_218, 2_689],
50+
["🇷🇺", "Russia (россия)", 143_997_000, 17.1, 9, 2_076_396, 14_258],
51+
["🇪🇹", "Ethiopia (እትዮጵያ)", 135_472_000, 1.1, 120, 117_457, 1_066],
52+
]
53+
COUNTRY_TITLE = "10 Largest Countries by Population 2025"
54+
COUNTRY_CAPTION = "Data from https://worldpopulationreview.com/ and Wikipedia"
55+
56+
57+
class TableApp(cmd2.Cmd):
58+
"""Cmd2 application to demonstrate displaying tabular data using rich."""
59+
60+
TABLE_CATEGORY = 'Table Commands'
61+
62+
def __init__(self) -> None:
63+
"""Initialize the cmd2 application."""
64+
super().__init__()
65+
66+
# Prints an intro banner once upon application startup
67+
self.intro = 'Are you curious which countries and cities on Earth have the largest populations?'
68+
69+
# Set the default category name
70+
self.default_category = 'cmd2 Built-in Commands'
71+
72+
@cmd2.with_category(TABLE_CATEGORY)
73+
def do_cities(self, _: cmd2.Statement) -> None:
74+
"""Display the cities with the largest population."""
75+
table = Table(title=CITY_TITLE, caption=CITY_CAPTION)
76+
77+
for header in CITY_HEADERS:
78+
table.add_column(header)
79+
80+
for row in CITY_DATA:
81+
# Convert integers or floats to strings, since rich tables can not render int/float
82+
str_row = [f"{item:,}" if isinstance(item, int) else str(item) for item in row]
83+
table.add_row(*str_row)
84+
85+
self.poutput(table)
86+
87+
@cmd2.with_category(TABLE_CATEGORY)
88+
def do_countries(self, _: cmd2.Statement) -> None:
89+
"""Display the countries with the largest population."""
90+
table = Table(title=COUNTRY_TITLE, caption=COUNTRY_CAPTION)
91+
92+
for header in COUNTRY_HEADERS:
93+
justify = "right"
94+
header_style = None
95+
style = None
96+
match header:
97+
case population if "2025 Population" in population:
98+
header_style = Color.BRIGHT_BLUE
99+
style = Color.BLUE
100+
case density if "Density" in density:
101+
header_style = Color.BRIGHT_RED
102+
style = Color.RED
103+
case percap if "per capita" in percap:
104+
header_style = Color.BRIGHT_GREEN
105+
style = Color.GREEN
106+
case flag if 'Flag' in flag:
107+
justify = "center"
108+
case country if 'Country' in country:
109+
justify = "left"
110+
111+
table.add_column(header, justify=justify, header_style=header_style, style=style)
112+
113+
for row in COUNTRY_DATA:
114+
# Convert integers or floats to strings, since rich tables can not render int/float
115+
str_row = [f"{item:,}" if isinstance(item, int) else str(item) for item in row]
116+
table.add_row(*str_row)
117+
118+
self.poutput(table)
119+
120+
121+
if __name__ == '__main__':
122+
app = TableApp()
123+
app.cmdloop()

0 commit comments

Comments
 (0)