1
- tableformatter: tabular data formatter
2
- ======================================
1
+ # tableformatter: tabular data formatter
3
2
[ ![ Latest Version] ( https://img.shields.io/pypi/v/tableformatter.svg?style=flat-square&label=latest%20stable%20version )] ( https://pypi.org/project/tableformatter/ )
4
3
[ ![ Build status] ( https://api.travis-ci.com/python-tableformatter/tableformatter.svg?branch=master )] ( https://travis-ci.com/python-tableformatter/tableformatter )
5
4
[ ![ Appveyor build status] ( https://img.shields.io/appveyor/ci/anselor/tableformatter.svg?style=flat-square&label=windows%20build )] ( https://ci.appveyor.com/project/anselor/tableformatter )
@@ -16,8 +15,8 @@ receive arbitrary Python objects.
16
15
17
16
[ ![ Screenshot] ( tf.png )] ( https://github.com/python-tableformatter/tableformatter/blob/master/tf.png )
18
17
19
- Main Features
20
- -------------
18
+
19
+ ## Main Features
21
20
- Easy to display simple tables with just one function call when you don't need the fine-grained control
22
21
- Fine-grained control of almost every aspect of how data is formatted when you want it
23
22
- Tables with column headers
@@ -28,14 +27,13 @@ Main Features
28
27
- Support for Python 3.4+ on Windows, macOS, and Linux
29
28
30
29
31
- Installation
32
- ============
30
+ ## Installation
33
31
``` Bash
34
32
pip install tableformatter
35
33
```
36
34
37
- Dependencies
38
- ------------
35
+
36
+ ## Dependencies
39
37
`` tableformatter `` depends on the [ wcwidth] ( https://github.com/jquast/wcwidth ) module for measuring the width of
40
38
unicode strings rendered to a terminal.
41
39
@@ -46,20 +44,19 @@ If you wish to use the optional support for color, then at least one of the foll
46
44
If both `` colorama `` and `` colored `` are installed, then `` colored `` will take precedence.
47
45
48
46
49
- Usage
50
- =====
47
+ ## Usage
51
48
For simple cases, you only need to use a single function from this module: `` generate_table `` . The only required argument
52
49
to this function is `` rows `` which is an Iterable of Iterables such as a list of lists or another tabular data type like
53
50
a 2D [ numpy] ( http://www.numpy.org ) array. `` generate_table `` outputs a nicely formatted table:
54
51
55
52
``` Python
56
- from tableformatter import generate_table
53
+ import tableformatter as tf
57
54
58
55
rows = [(' A1' , ' A2' , ' A3' , ' A4' ),
59
56
(' B1' , ' B2\n B2\n B2' , ' B3' , ' B4' ),
60
57
(' C1' , ' C2' , ' C3' , ' C4' ),
61
58
(' D1' , ' D2' , ' D3' , ' D4' )]
62
- print (generate_table(rows))
59
+ print (tf. generate_table(rows))
63
60
╔════╤════╤════╤════╗
64
61
║ A1 │ A2 │ A3 │ A4 ║
65
62
║ B1 │ B2 │ B3 │ B4 ║
@@ -72,13 +69,13 @@ print(generate_table(rows))
72
69
73
70
* NOTE: Rendering of tables looks much better in Python than it appears in this Markdown file.*
74
71
75
- Column Headers
76
- --------------
72
+
73
+ ## Column Headers
77
74
The second argument to `` generate_table `` named `` columns `` is optional and defines a list of column headers to be used.
78
75
79
76
``` Python
80
77
cols = [' Col1' , ' Col2' , ' Col3' , ' Col4' ]
81
- print (generate_table(rows, cols))
78
+ print (tf. generate_table(rows, cols))
82
79
╔══════╤══════╤══════╤══════╗
83
80
║ Col1 │ Col2 │ Col3 │ Col4 ║
84
81
╠══════╪══════╪══════╪══════╣
@@ -91,8 +88,8 @@ print(generate_table(rows, cols))
91
88
╚══════╧══════╧══════╧══════╝
92
89
```
93
90
94
- Grid Style
95
- ----------
91
+
92
+ ## Grid Style
96
93
The third argument to `` generated `` table named `` grid_style `` is optional and specifies how the table lines are drawn.
97
94
98
95
Supported grid sytles are:
@@ -102,9 +99,7 @@ Supported grid sytles are:
102
99
* ** SparseGrid** - sparse grid with no lines at all to conserve both vertical and horizontal space
103
100
104
101
``` Python
105
- from tableformatter import FancyGrid
106
-
107
- print (generate_table(rows, grid_style = FancyGrid()))
102
+ print (tf.generate_table(rows, grid_style = tf.FancyGrid()))
108
103
╔════╤════╤════╤════╗
109
104
║ A1 │ A2 │ A3 │ A4 ║
110
105
╟────┼────┼────┼────╢
@@ -118,14 +113,14 @@ print(generate_table(rows, grid_style=FancyGrid()))
118
113
╚════╧════╧════╧════╝
119
114
```
120
115
121
- Transposed Tables
122
- -----------------
116
+
117
+ ## Transposed Tables
123
118
Normally the "rows" are displayed left-to-right and "columns" are displayed up-to-down. However, if you want to transpose
124
119
this and print "rows" up-to-down and "columns" left-to-right then that is easily done using the fourth (optional) argument
125
120
to `` generate_table `` :
126
121
127
122
``` Python
128
- print (generate_table(rows, cols, transpose = True ))
123
+ print (tf. generate_table(rows, cols, transpose = True ))
129
124
╔══════╦════╤════╤════╤════╗
130
125
║ Col1 ║ A1 │ B1 │ C1 │ D1 ║
131
126
║ Col2 ║ A2 │ B2 │ C2 │ D2 ║
@@ -136,14 +131,118 @@ print(generate_table(rows, cols, transpose=True))
136
131
╚══════╩════╧════╧════╧════╝
137
132
```
138
133
139
- Column Alignment
140
- ----------------
141
- TODO
142
134
143
- Number Formatting
144
- -----------------
145
- TODO
135
+ ## Column Formatting
136
+ For rich column formatting, instead of passing a simple iterable to the 2nd argument of `` generate_table `` , an iterable
137
+ of `` tableformatter.Column `` objects can be passed. The `` Column `` class has the following attributes, all of which are
138
+ optional other than the first:
139
+
140
+ | Attribute name | Description |
141
+ | -------------- | ----------- |
142
+ | col_name | Column name to display
143
+ | width | Number of displayed terminal characters. Unicode wide characters count as 2 displayed characters.
144
+ | attrib | The name of the object attribute to look up for cell contents on this column
145
+ | wrap_mode | Defines how to handle long cells that must be wrapped or truncated
146
+ | wrap_prefix | String to display at the beginning of each wrapped line in a cell
147
+ | cell_padding | Number of padding spaces to the left and right of each cell
148
+ | header_halign | Horizontal alignment of the column header
149
+ | header_valign | Vertical alignment of the column header
150
+ | cell_halign | Horizontal alignment of the cells in this column
151
+ | cell_valign | Vertical alignment of the cells in this column
152
+ | formatter | Callable that can process the value in this column for display
153
+ | obj_formatter | Callable that processes the row object to generate content for this column
154
+
155
+ See the [ columns.py] ( https://github.com/python-tableformatter/tableformatter/blob/master/examples/columns.py ) example
156
+ for a demonstration for how all of these options can be used.
157
+
158
+ ### Column Alignment
159
+ You can override both the horizontal and vertical alignment. This can be done separately for the column header versus
160
+ the cell contents.
161
+
162
+ Possible alignments are all elements within the `` ColumnAlignment `` enum class:
163
+ * Horizontal alignments:
164
+ * AlignLeft (* default* )
165
+ * AlignCenter
166
+ * AlignRight
167
+ * Vertical alignments:
168
+ * AlignTop (* default* )
169
+ * AlignBottom
170
+
171
+ ``` Python
172
+ columns = (tf.Column(' Col1' , cell_halign = tf.ColumnAlignment.AlignLeft),
173
+ tf.Column(' Col2' , cell_halign = tf.ColumnAlignment.AlignRight),
174
+ tf.Column(' Col3' , cell_halign = tf.ColumnAlignment.AlignCenter),
175
+ tf.Column(' Col4' , cell_valign = tf.ColumnAlignment.AlignBottom))
176
+ print (tf.generate_table(rows, columns))
177
+ ╔══════╤══════╤══════╤══════╗
178
+ ║ Col1 │ Col2 │ Col3 │ col4 ║
179
+ ╠══════╪══════╪══════╪══════╣
180
+ ║ A1 │ A2 │ A3 │ A4 ║
181
+ ║ B1 │ B2 │ B3 │ ║
182
+ ║ │ B2 │ │ ║
183
+ ║ │ B2 │ │ B4 ║
184
+ ║ C1 │ C2 │ C3 │ C4 ║
185
+ ║ D1 │ D2 │ D3 │ D4 ║
186
+ ╚══════╧══════╧══════╧══════╝
187
+ ```
188
+
189
+ ### Number Formatting
190
+ The `` formatter `` attribute of the `` Column `` class accepts an arbitrary callable which accepts a single argument containing
191
+ the cell contents for that column and returns a string with the contents formatted in the
192
+ desired format. There are a couple callable classes built into `` tableformatter `` to help
193
+ formatting numbers:
194
+ * FormatBytes - formats a value in bytes into a human readable string
195
+ * FormatCommas - Formats a number with comma separators
196
+
197
+ ``` Python
198
+ rows = [(None , None ),
199
+ (' 123' , ' 123' ),
200
+ (123 , 123 ),
201
+ (12345 , 12345 ),
202
+ (12345678 , 12345678 ),
203
+ (1234567890 , 1234567890 ),
204
+ (1234567890123 , 1234567890123 )]
205
+ cols = (tf.Column(' First' , width = 20 , formatter = tf.FormatBytes(), cell_halign = tf.ColumnAlignment.AlignRight),
206
+ tf.Column(' Second' , formatter = tf.FormatCommas(), cell_halign = tf.ColumnAlignment.AlignRight))
207
+ print (tf.generate_table(rows, cols))
208
+ ╔═══════════╤═══════════════════╗
209
+ ║ First │ Second ║
210
+ ╠═══════════╪═══════════════════╣
211
+ ║ │ ║
212
+ ║ 123.00 B │ 123 ║
213
+ ║ 123.00 B │ 123 ║
214
+ ║ 12.06 KB │ 12 ,345 ║
215
+ ║ 11.77 MB │ 12 ,345 ,678 ║
216
+ ║ 1.15 GB │ 1 ,234 ,567 ,890 ║
217
+ ║ 1.12 TB │ 1 ,234 ,567 ,890 ,123 ║
218
+ ╚═══════════╧═══════════════════╝
219
+ ```
220
+
221
+ See the [ cmd2_tables.py] ( https://github.com/python-tableformatter/tableformatter/blob/master/examples/formatters.py ) example
222
+ for examples of how custom formatter functions can be used.
223
+
224
+
225
+ ## Color
226
+ If either `` colorama `` or `` colored `` is installed, then `` tableformatter `` has support for altering both the foreground
227
+ and background color of cell contents.
228
+
229
+ ### Alternating background row color
230
+ It is trivial to alternate the background color of each row as follows:
231
+ ``` Python
232
+ from colorama import Back
233
+ print (generate_table(rows, cols, grid_style = tf.AlternatingRowGrid(Back.GREEN , Back.BLUE )))
234
+ ```
235
+ See the [ cmd2_tables.py] ( https://github.com/python-tableformatter/tableformatter/blob/master/examples/cmd2_tables.py ) or
236
+ [ color.py] ( https://github.com/python-tableformatter/tableformatter/blob/master/examples/color.py ) examples for more
237
+ complete examples.
238
+
239
+ ### Dynamically setting cell color based on cell contents
240
+ The 5th argument to `` generate_table `` is ** row_tagger** and it expects a callable which accepts a single argument for
241
+ the row contents and returns a dictionary of row options for that row which can
242
+ be used to set the foreground and/or background color of all cells in that row.
243
+
244
+ See the `` high_density_tuples `` function in the [ cmd2_tables.py] ( https://github.com/python-tableformatter/tableformatter/blob/master/examples/cmd2_tables.py )
245
+ example for a demonstration of how to use this feature.
246
+
247
+
146
248
147
- Color
148
- -----
149
- TODO
0 commit comments