Skip to content

Commit 6d86f02

Browse files
authored
Merge pull request #3 from python-tableformatter/readme
Added content to README.md
2 parents b39b444 + 5570554 commit 6d86f02

File tree

3 files changed

+142
-6
lines changed

3 files changed

+142
-6
lines changed

README.md

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,135 @@
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

TableFormatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ def cell_format(text):
512512

513513

514514
class SparseGrid(object):
515-
"""Fancy table with grid lines dividing rows and columns"""
515+
"""Sparse grid with no lines at all and no alternating background colors"""
516516
can_wrap = True
517517
show_header = False
518518

setup.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
VERSION = '0.0.1'
99
DESCRIPTION = "python-tableformatter - Tabular data formatter allowing printing from both arbitrary tuples of strings or object inspection"
10-
LONG_DESCRIPTION = """
10+
LONG_DESCRIPTION = """tableformatter is a tabular data formatter allowing printing from both arbitrary tuples of strings or object inspection.
11+
It converts your data into a string form suitable for pretty-printing as a table. The goal is to make it quick and easy
12+
for developers to display tabular data in an aesthetically pleasing fashion. It provides a simple public API, but allows
13+
fine-grained control over almost every aspect of how the data is formatted.
1114
"""
1215

1316
CLASSIFIERS = list(filter(None, map(str.strip,
@@ -30,10 +33,9 @@
3033
EXTRAS_REQUIRE = {
3134
# POSIX OSes also require wcwidth for correctly estimating the displayed width of unicode chars
3235
":sys_platform!='win32'": ['wcwidth'],
33-
# Python 3.4 and earlier require contextlib2 for temporarily redirecting stderr and stdout
36+
# Python 3.4 and earlier the typing module backport for optional type hinting support
3437
":python_version<'3.5'": ['typing'],
35-
# development only dependencies
36-
# install with 'pip install -e .[dev]'
38+
# development only dependencies - install with 'pip install -e .[dev]'
3739
'dev': [
3840
'pytest', 'pytest-cov', 'tox', 'pylint', 'sphinx', 'sphinx-rtd-theme',
3941
'sphinx-autobuild', 'invoke', 'twine>=1.11',

0 commit comments

Comments
 (0)