Skip to content

Commit 75f2199

Browse files
committed
Added content to README.md
Also: - Added long description to setup.py - Added colorama as a dependency to setup.py - Fixed a typo in a docstring in SparseGrid
1 parent b39b444 commit 75f2199

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

README.md

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,133 @@
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+
Installation
20+
============
21+
```Bash
22+
pip install tableformatter
23+
```
24+
25+
Dependencies
26+
------------
27+
``tableformatter`` requires that at least one of the following two modules are installed to support table colors:
28+
* [colorama](https://github.com/tartley/colorama)
29+
* [colored](https://github.com/dslackw/colored)
30+
31+
**pip** will treat ``colorama`` as a dependency and install if it isn't already installed. However, if ``colored`` is
32+
installed, then it will be used instead of ``colorama``.
33+
34+
35+
Usage
36+
=====
37+
For simple cases, you only need to use a single function from this module: ``generate_table``. The only required argument
38+
to this function is ``rows`` which is an Iterable of Iterables such as a list of lists or another tabular data type like
39+
a 2D [numpy](http://www.numpy.org) array. ``generate_table`` outputs a nicely formatted table:
40+
41+
```Python
42+
>>> from TableFormatter import generate_table
43+
44+
>>> rows = [('A1', 'A2', 'A3', 'A4'),
45+
... ('B1', 'B2\nB2\nB2', 'B3', 'B4'),
46+
... ('C1', 'C2', 'C3', 'C4'),
47+
... ('D1', 'D2', 'D3', 'D4')]
48+
>>> print(generate_table(rows))
49+
╔════╤════╤════╤════╗
50+
║ A1 │ A2 │ A3 │ A4 ║
51+
║ B1 │ B2 │ B3 │ B4 ║
52+
║ │ B2 │ │ ║
53+
║ │ B2 │ │ ║
54+
║ C1 │ C2 │ C3 │ C4 ║
55+
║ D1 │ D2 │ D3 │ D4 ║
56+
╚════╧════╧════╧════╝
57+
```
58+
59+
Column Headers
60+
--------------
61+
The second argument to ``generate_table`` named ``columns`` is optional and defines a list of column headers to be used.
62+
63+
```Python
64+
>>> cols = ['Col1', 'Col2', 'Col3', 'Col4']
65+
>>> print(generate_table(rows, cols))
66+
╔══════╤══════╤══════╤══════╗
67+
║ Col1 │ Col2 │ Col3 │ Col4 ║
68+
╠══════╪══════╪══════╪══════╣
69+
║ A1 │ A2 │ A3 │ A4 ║
70+
║ B1 │ B2 │ B3 │ B4 ║
71+
║ │ B2 │ │ ║
72+
║ │ B2 │ │ ║
73+
║ C1 │ C2 │ C3 │ C4 ║
74+
║ D1 │ D2 │ D3 │ D4 ║
75+
╚══════╧══════╧══════╧══════╝
76+
```
77+
78+
Grid Style
79+
----------
80+
The third argument to ``generated`` table named ``grid_style`` is optional and specifies how the table lines are drawn.
81+
82+
Supported grid sytles are:
83+
84+
* **AlternatingRowGrid** - generates alternating black/gray background colors for rows to conserve vertical space (defalt)
85+
* **FancyGrid** - fancy table with grid lines dividing rows and columns
86+
* **SparseGrid** - sparse grid with no lines at all to conserve both vertical and horizontal space
87+
88+
```Python
89+
>>> from TableFormatter import FancyGrid
90+
91+
>>> print(generate_table(rows, grid_style=FancyGrid))
92+
╔════╤════╤════╤════╗
93+
║ A1 │ A2 │ A3 │ A4 ║
94+
╟────┼────┼────┼────╢
95+
║ B1 │ B2 │ B3 │ B4 ║
96+
║ │ B2 │ │ ║
97+
║ │ B2 │ │ ║
98+
╟────┼────┼────┼────╢
99+
║ C1 │ C2 │ C3 │ C4 ║
100+
╟────┼────┼────┼────╢
101+
║ D1 │ D2 │ D3 │ D4 ║
102+
╚════╧════╧════╧════╝
103+
```
104+
105+
Transposed Tables
106+
-----------------
107+
Normally the "rows" are displayed left-to-right and "columns" are displayed up-to-down. However, if you want to transpose
108+
this and print "rows" up-to-down and "columns" left-to-right then that is easily done using the fourth (optional) argument
109+
to ``generate_table``:
110+
111+
```Python
112+
>>> print(generate_table(rows, cols, transpose=True))
113+
╔══════╦════╤════╤════╤════╗
114+
║ Col1 ║ A1 │ B1 │ C1 │ D1 ║
115+
║ Col2 ║ A2 │ B2 │ C2 │ D2 ║
116+
║ ║ │ B2 │ │ ║
117+
║ ║ │ B2 │ │ ║
118+
║ Col3 ║ A3 │ B3 │ C3 │ D3 ║
119+
║ Col4 ║ A4 │ B4 │ C4 │ D4 ║
120+
╚══════╩════╧════╧════╧════╝
121+
```
122+
123+
Column Alignment
124+
----------------
125+
TODO
126+
127+
Number Formatting
128+
-----------------
129+
TODO
130+
131+
Color
132+
-----
133+
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: 7 additions & 1 deletion
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,
@@ -27,6 +30,8 @@
2730
Topic :: Software Development :: Libraries :: Python Modules
2831
""".splitlines())))
2932

33+
INSTALL_REQUIRES = ['colorama']
34+
3035
EXTRAS_REQUIRE = {
3136
# POSIX OSes also require wcwidth for correctly estimating the displayed width of unicode chars
3237
":sys_platform!='win32'": ['wcwidth'],
@@ -54,5 +59,6 @@
5459
py_modules = ['TableFormatter'],
5560
keywords='table tabular formatter',
5661
python_requires='>=3.4',
62+
install_requires=INSTALL_REQUIRES,
5763
extras_require=EXTRAS_REQUIRE,
5864
)

0 commit comments

Comments
 (0)