|
| 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 | +""" |
| 9 | + |
| 10 | +from rich.table import Table |
| 11 | + |
| 12 | +import cmd2 |
| 13 | + |
| 14 | +CITY_HEADERS = ['Country Flag', 'City', 'Country', '2025 Population'] |
| 15 | + |
| 16 | +CITY_DATA = [ |
| 17 | + [ |
| 18 | + "🇯🇵", |
| 19 | + "Tokyo (東京)", |
| 20 | + "Japan", |
| 21 | + 37_036_200, |
| 22 | + ], |
| 23 | + [ |
| 24 | + "🇮🇳", |
| 25 | + "Delhi (दिल्ली)", |
| 26 | + "India", |
| 27 | + 34_665_600, |
| 28 | + ], |
| 29 | + [ |
| 30 | + "🇨🇳", |
| 31 | + "Shanghai (上海)", |
| 32 | + "China", |
| 33 | + 30_482_100, |
| 34 | + ], |
| 35 | + [ |
| 36 | + "🇧🇩", |
| 37 | + "Dhaka (ঢাকা)", |
| 38 | + "Bangladesh", |
| 39 | + 24_652_900, |
| 40 | + ], |
| 41 | + [ |
| 42 | + "🇪🇬", |
| 43 | + "Cairo (القاهرة)", |
| 44 | + "Egypt", |
| 45 | + 23_074_200, |
| 46 | + ], |
| 47 | + [ |
| 48 | + "🇪🇬", |
| 49 | + "São Paulo", |
| 50 | + "Brazil", |
| 51 | + 22_990_000, |
| 52 | + ], |
| 53 | +] |
| 54 | + |
| 55 | +COUNTRY_HEADERS = ['Flag', 'Country', '2025 Population', 'Area (M km^2)', 'Density (/km^2)'] |
| 56 | + |
| 57 | +COUNTRY_DATA = [ |
| 58 | + [ |
| 59 | + "🇮🇳", |
| 60 | + "India", |
| 61 | + 1_463_870_000, |
| 62 | + 3.3, |
| 63 | + 492, |
| 64 | + ], |
| 65 | + [ |
| 66 | + "🇨🇳", |
| 67 | + "China", |
| 68 | + 1_416_100_000, |
| 69 | + 9.7, |
| 70 | + 150, |
| 71 | + ], |
| 72 | + [ |
| 73 | + "🇺🇸", |
| 74 | + "United States", |
| 75 | + 347_276_000, |
| 76 | + 9.4, |
| 77 | + 38, |
| 78 | + ], |
| 79 | + [ |
| 80 | + "🇮🇩", |
| 81 | + "Indonesia", |
| 82 | + 285_721_000, |
| 83 | + 1.9, |
| 84 | + 152, |
| 85 | + ], |
| 86 | + [ |
| 87 | + "🇵🇰", |
| 88 | + "Pakistan", |
| 89 | + 255_220_000, |
| 90 | + 0.9, |
| 91 | + 331, |
| 92 | + ], |
| 93 | + [ |
| 94 | + "🇳🇬", |
| 95 | + "Nigeria", |
| 96 | + 237_528_000, |
| 97 | + 0.9, |
| 98 | + 261, |
| 99 | + ], |
| 100 | +] |
| 101 | + |
| 102 | + |
| 103 | +class TableApp(cmd2.Cmd): |
| 104 | + """Cmd2 application to demonstrate displaying tabular data using rich.""" |
| 105 | + |
| 106 | + TABLE_CATEGORY = 'Table Commands' |
| 107 | + |
| 108 | + def __init__(self) -> None: |
| 109 | + """Initialize the cmd2 application.""" |
| 110 | + super().__init__() |
| 111 | + |
| 112 | + # Prints an intro banner once upon application startup |
| 113 | + self.intro = 'Are you curious which countries and cities on Earth have the largest populations?' |
| 114 | + |
| 115 | + # Set the default category name |
| 116 | + self.default_category = 'cmd2 Built-in Commands' |
| 117 | + |
| 118 | + @cmd2.with_category(TABLE_CATEGORY) |
| 119 | + def do_cities(self, _: cmd2.Statement) -> None: |
| 120 | + """Display the cities with the largest population.""" |
| 121 | + table = Table(show_footer=False) |
| 122 | + table.title = "Largest Cities by Population 2025" |
| 123 | + table.caption = "Data from https://worldpopulationreview.com/" |
| 124 | + |
| 125 | + for header in CITY_HEADERS: |
| 126 | + table.add_column(header) |
| 127 | + |
| 128 | + for row in CITY_DATA: |
| 129 | + # Convert integers or floats to strings, since rich tables can not render int/float |
| 130 | + str_row = [f"{item:,}" if isinstance(item, int) else str(item) for item in row] |
| 131 | + table.add_row(*str_row) |
| 132 | + |
| 133 | + self.poutput(table) |
| 134 | + |
| 135 | + @cmd2.with_category(TABLE_CATEGORY) |
| 136 | + def do_countries(self, _: cmd2.Statement) -> None: |
| 137 | + """Display the countries with the largest population.""" |
| 138 | + table = Table(show_footer=False) |
| 139 | + table.title = "Largest Countries by Population 2025" |
| 140 | + table.caption = "Data from https://worldpopulationreview.com/" |
| 141 | + |
| 142 | + for header in COUNTRY_HEADERS: |
| 143 | + justify = "left" |
| 144 | + if 'Population' in header or 'Density' in header: |
| 145 | + justify = "right" |
| 146 | + if 'Area' in header: |
| 147 | + justify = "center" |
| 148 | + table.add_column(header, justify=justify) |
| 149 | + |
| 150 | + for row in COUNTRY_DATA: |
| 151 | + # Convert integers or floats to strings, since rich tables can not render int/float |
| 152 | + str_row = [f"{item:,}" if isinstance(item, int) else str(item) for item in row] |
| 153 | + table.add_row(*str_row) |
| 154 | + |
| 155 | + self.poutput(table) |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == '__main__': |
| 159 | + app = TableApp() |
| 160 | + app.cmdloop() |
0 commit comments