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