|
11 | 11 | WARNING: This example requires the tableformatter module: https://github.com/python-tableformatter/tableformatter |
12 | 12 | - pip install tableformatter |
13 | 13 | """ |
14 | | -import functools |
| 14 | +import argparse |
15 | 15 |
|
16 | 16 | import cmd2 |
17 | 17 | import tableformatter as tf |
|
30 | 30 | BACK_ALT = '' |
31 | 31 |
|
32 | 32 | # Format to use for the grid style when displaying tables with the tableformatter module |
33 | | -grid_style = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide |
34 | | - |
35 | | -# Create a function to format a fixed-width table for pretty-printing using the desired table format |
36 | | -table = functools.partial(tf.generate_table, grid_style=grid_style) |
| 33 | +DEFAULT_GRID = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) # Use rows of alternating color to assist as a visual guide |
37 | 34 |
|
38 | 35 |
|
39 | 36 | # Formatter functions |
40 | | - |
41 | 37 | def no_dec(num: float) -> str: |
42 | 38 | """Format a floating point number with no decimal places.""" |
43 | 39 | return "{}".format(round(num)) |
@@ -136,28 +132,40 @@ class TableDisplay(cmd2.Cmd): |
136 | 132 | def __init__(self): |
137 | 133 | super().__init__() |
138 | 134 |
|
139 | | - def ptable(self, tabular_data, headers=()): |
| 135 | + def ptable(self, rows, columns, grid_args): |
140 | 136 | """Format tabular data for pretty-printing as a fixed-width table and then display it using a pager. |
141 | 137 |
|
142 | | - :param tabular_data: required argument - can be a list-of-lists (or another iterable of iterables), a list of |
143 | | - named tuples, a dictionary of iterables, an iterable of dictionaries, a two-dimensional |
144 | | - NumPy array, NumPy record array, or a Pandas dataframe. |
145 | | - :param headers: (optional) - to print nice column headers, supply this argument: |
146 | | - - headers can be an explicit list of column headers |
147 | | - - if `headers="firstrow"`, then the first row of data is used |
148 | | - - if `headers="keys"`, then dictionary keys or column indices are used |
149 | | - - Otherwise, a headerless table is produced |
| 138 | + :param rows: required argument - can be a list-of-lists (or another iterable of iterables), a two-dimensional |
| 139 | + NumPy array, or an Iterable of non-iterable objects |
| 140 | + :param columns: column headers and formatting options per column |
| 141 | + :param grid_args: argparse arguments for formatting the grid |
150 | 142 | """ |
151 | | - formatted_table = table(rows=tabular_data, columns=headers) |
| 143 | + if grid_args.color: |
| 144 | + grid = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT) |
| 145 | + elif grid_args.fancy: |
| 146 | + grid = tf.FancyGrid() |
| 147 | + elif grid_args.sparse: |
| 148 | + grid = tf.SparseGrid() |
| 149 | + else: |
| 150 | + grid = None |
| 151 | + |
| 152 | + formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid) |
152 | 153 | self.ppaged(formatted_table, chop=True) |
153 | 154 |
|
154 | | - def do_table(self, _): |
| 155 | + table_parser = argparse.ArgumentParser() |
| 156 | + table_parser.add_argument('-c', '--color', action='store_true', help='Enable color') |
| 157 | + table_parser.add_argument('-f', '--fancy', action='store_true', help='Fancy Grid') |
| 158 | + table_parser.add_argument('-s', '--sparse', action='store_true', help='Sparse Grid') |
| 159 | + |
| 160 | + @cmd2.with_argparser(table_parser) |
| 161 | + def do_table(self, args): |
155 | 162 | """Display data on the Earth's most populated cities in a table.""" |
156 | | - self.ptable(EXAMPLE_ITERABLE_DATA, columns) |
| 163 | + self.ptable(EXAMPLE_ITERABLE_DATA, columns, args) |
157 | 164 |
|
158 | | - def do_object_table(self, _): |
| 165 | + @cmd2.with_argparser(table_parser) |
| 166 | + def do_object_table(self, args): |
159 | 167 | """Display data on the Earth's most populated cities in a table.""" |
160 | | - self.ptable(EXAMPLE_OBJECT_DATA, obj_cols) |
| 168 | + self.ptable(EXAMPLE_OBJECT_DATA, obj_cols, args) |
161 | 169 |
|
162 | 170 |
|
163 | 171 | if __name__ == '__main__': |
|
0 commit comments