| 1 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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() |