|
1 | | -# tableformatter |
| 1 | +tableformatter: tabular data formatter |
| 2 | +====================================== |
| 3 | +tableformatter is a tabular data formatter allowing printing from both arbitrary tuples of strings or object inspection. |
| 4 | +It converts your data into a string form suitable for pretty-printing as a table. The goal is to make it quick and easy |
| 5 | +for developers to display tabular data in an aesthetically pleasing fashion. It provides a simple public API, but allows |
| 6 | +fine-grained control over almost every aspect of how the data is formatted. |
| 7 | + |
| 8 | +Main Features |
| 9 | +------------- |
| 10 | +- Easy to display simple tables with just one function call when you don't need the fine-grained control |
| 11 | +- Fine-grained control of almost every aspect of how data is formatted when you want it |
| 12 | +- Tables with column headers |
| 13 | +- Flexible grid style |
| 14 | +- Transposed tables with rows and columns swapped |
| 15 | +- Colored output using either [colorama](https://github.com/tartley/colorama) or [colored](https://github.com/dslackw/colored) |
| 16 | +- Good unicode support including for characters which are more than 1 visual character wide |
| 17 | +- Support for Python 3.4+ on Windows, macOS, and Linux |
| 18 | + |
| 19 | + |
| 20 | +Installation |
| 21 | +============ |
| 22 | +```Bash |
| 23 | +pip install tableformatter |
| 24 | +``` |
| 25 | + |
| 26 | +Dependencies |
| 27 | +------------ |
| 28 | +``tableformatter`` does not have any required dependencies. |
| 29 | + |
| 30 | +However, if you wish to use the provided optional support for color, then at least one of the following two modules must be installed: |
| 31 | +* [colorama](https://github.com/tartley/colorama) |
| 32 | +* [colored](https://github.com/dslackw/colored) |
| 33 | + |
| 34 | +If both ``colorama`` and ``colored`` are installed, then ``colored`` will take precedence. |
| 35 | + |
| 36 | + |
| 37 | +Usage |
| 38 | +===== |
| 39 | +For simple cases, you only need to use a single function from this module: ``generate_table``. The only required argument |
| 40 | +to this function is ``rows`` which is an Iterable of Iterables such as a list of lists or another tabular data type like |
| 41 | +a 2D [numpy](http://www.numpy.org) array. ``generate_table`` outputs a nicely formatted table: |
| 42 | + |
| 43 | +```Python |
| 44 | +>>> from TableFormatter import generate_table |
| 45 | + |
| 46 | +>>> rows = [('A1', 'A2', 'A3', 'A4'), |
| 47 | +... ('B1', 'B2\nB2\nB2', 'B3', 'B4'), |
| 48 | +... ('C1', 'C2', 'C3', 'C4'), |
| 49 | +... ('D1', 'D2', 'D3', 'D4')] |
| 50 | +>>> print(generate_table(rows)) |
| 51 | +╔════╤════╤════╤════╗ |
| 52 | +║ A1 │ A2 │ A3 │ A4 ║ |
| 53 | +║ B1 │ B2 │ B3 │ B4 ║ |
| 54 | +║ │ B2 │ │ ║ |
| 55 | +║ │ B2 │ │ ║ |
| 56 | +║ C1 │ C2 │ C3 │ C4 ║ |
| 57 | +║ D1 │ D2 │ D3 │ D4 ║ |
| 58 | +╚════╧════╧════╧════╝ |
| 59 | +``` |
| 60 | + |
| 61 | +Column Headers |
| 62 | +-------------- |
| 63 | +The second argument to ``generate_table`` named ``columns`` is optional and defines a list of column headers to be used. |
| 64 | + |
| 65 | +```Python |
| 66 | +>>> cols = ['Col1', 'Col2', 'Col3', 'Col4'] |
| 67 | +>>> print(generate_table(rows, cols)) |
| 68 | +╔══════╤══════╤══════╤══════╗ |
| 69 | +║ Col1 │ Col2 │ Col3 │ Col4 ║ |
| 70 | +╠══════╪══════╪══════╪══════╣ |
| 71 | +║ A1 │ A2 │ A3 │ A4 ║ |
| 72 | +║ B1 │ B2 │ B3 │ B4 ║ |
| 73 | +║ │ B2 │ │ ║ |
| 74 | +║ │ B2 │ │ ║ |
| 75 | +║ C1 │ C2 │ C3 │ C4 ║ |
| 76 | +║ D1 │ D2 │ D3 │ D4 ║ |
| 77 | +╚══════╧══════╧══════╧══════╝ |
| 78 | +``` |
| 79 | + |
| 80 | +Grid Style |
| 81 | +---------- |
| 82 | +The third argument to ``generated`` table named ``grid_style`` is optional and specifies how the table lines are drawn. |
| 83 | + |
| 84 | +Supported grid sytles are: |
| 85 | + |
| 86 | +* **AlternatingRowGrid** - generates alternating black/gray background colors for rows to conserve vertical space (defalt) |
| 87 | +* **FancyGrid** - fancy table with grid lines dividing rows and columns |
| 88 | +* **SparseGrid** - sparse grid with no lines at all to conserve both vertical and horizontal space |
| 89 | + |
| 90 | +```Python |
| 91 | +>>> from TableFormatter import FancyGrid |
| 92 | + |
| 93 | +>>> print(generate_table(rows, grid_style=FancyGrid)) |
| 94 | +╔════╤════╤════╤════╗ |
| 95 | +║ A1 │ A2 │ A3 │ A4 ║ |
| 96 | +╟────┼────┼────┼────╢ |
| 97 | +║ B1 │ B2 │ B3 │ B4 ║ |
| 98 | +║ │ B2 │ │ ║ |
| 99 | +║ │ B2 │ │ ║ |
| 100 | +╟────┼────┼────┼────╢ |
| 101 | +║ C1 │ C2 │ C3 │ C4 ║ |
| 102 | +╟────┼────┼────┼────╢ |
| 103 | +║ D1 │ D2 │ D3 │ D4 ║ |
| 104 | +╚════╧════╧════╧════╝ |
| 105 | +``` |
| 106 | + |
| 107 | +Transposed Tables |
| 108 | +----------------- |
| 109 | +Normally the "rows" are displayed left-to-right and "columns" are displayed up-to-down. However, if you want to transpose |
| 110 | +this and print "rows" up-to-down and "columns" left-to-right then that is easily done using the fourth (optional) argument |
| 111 | +to ``generate_table``: |
| 112 | + |
| 113 | +```Python |
| 114 | +>>> print(generate_table(rows, cols, transpose=True)) |
| 115 | +╔══════╦════╤════╤════╤════╗ |
| 116 | +║ Col1 ║ A1 │ B1 │ C1 │ D1 ║ |
| 117 | +║ Col2 ║ A2 │ B2 │ C2 │ D2 ║ |
| 118 | +║ ║ │ B2 │ │ ║ |
| 119 | +║ ║ │ B2 │ │ ║ |
| 120 | +║ Col3 ║ A3 │ B3 │ C3 │ D3 ║ |
| 121 | +║ Col4 ║ A4 │ B4 │ C4 │ D4 ║ |
| 122 | +╚══════╩════╧════╧════╧════╝ |
| 123 | +``` |
| 124 | + |
| 125 | +Column Alignment |
| 126 | +---------------- |
| 127 | +TODO |
| 128 | + |
| 129 | +Number Formatting |
| 130 | +----------------- |
| 131 | +TODO |
| 132 | + |
| 133 | +Color |
| 134 | +----- |
| 135 | +TODO |
0 commit comments