Helper to pretty print an ascii table with a tree-like structure
Project description
treetable
Helper to pretty print an ascii table with a tree-like structure.
Installation and requirements
treetable requires at least python3.6.
pip3 install treetable
Quick example
treetable allows to easily output complex ascii tables like
|| || metrics
|| info || train | test
name || index status || Pr recall | auc accuracy
RirpUoE || 21 L || 94.4% 56.4% | 46.3% 79.6%
wtAYHBf || ||
j || ||
rLsITTK || 47 q || 66.0% 84.8% | 46.5% 64.9%
S || ||
Uumlvod || 49 Z || 63.1% 99.8% | 94.6% 10.6%
SmIsO || ||
rzXlDqM || 32 J || 48.8% 33.5% | 30.8% 94.2%
PyCX || ||
Usage and examples
The main function is treetable.treetable. It takes a tree-like structure
to represent the table. For instance, I could have a sub-table info and
a sub-table metrics, each one being recursively composed of other sub-tables.
Each extra level of sub-tables use a different separator (by default up to 3
levels but you can provide extra separators with the separators arguments).
At the leaf level of the tree, a format string (that can be passed to the
format builtin) can be specified. Let's take an example
from treetable import table, group, leaf
mytable = table([
group('info', [
leaf('name'),
leaf('index')]),
group('metrics', align='>', groups=[
leaf('speed', '.0f'),
leaf('accuracy', '.1%'),
leaf('special', '.1%', align='=')]),
])
The lines of the table should be provided following a list of nested dictionaries with the same shape, for instance:
lines = [
{'info': {'name': 'bob', 'index': 4}, 'metrics':{'speed': 200, 'accuracy': 0.21, 'special': 0.1}},
{'info': {'name': 'alice', 'index': 2}, 'metrics':{'speed': 67, 'accuracy': 0.45, 'special': 4.56}},
]
Now running print(treetable(lines, mytable)) will give you
info | metrics
name index | speed accuracy special
bob 4 | 200 21.0% 10.0%
alice 2 | 67 45.0% 456.0%
table, group and leaf are all node definition functions. They all
accept the same arguments and differ only in the order of positional arguments.
When defined in a leaf node, the arguments will directly influence
how the data is rendered. In group nodes or the root (aka table) node,
they will override the default behaviors in descendent leafs. The following
arguments are defined:
key: access key in thelinesdata structure.groups(only forgroupandtablenodes): list of sub-tables.display: display name used, when different to the name to access the value in thelinesstructure.align: alignment of text, either '<' (left aligned), '=' (centered) or '>' (right aligned).wrap: wrap text beyond a certain number of characters. No smart wrapping, this will wrap exactly at the limit characters by inserting a new line.missing: value used when a specific key is not present. Default is''.shorten: automatically shorten columns names. They are not shorten any more than the width of the underlying column and a long enough prefix is kept to remove any possible ambiguity with other columns in the same sub-tab le.
For instance, when using shorten=True with the above table:
info | metrics
name i | spee accur specia
bob 4 | 200 21.0% 10.0%
alice 2 | 67 45.0% 456.0%
name wasn't shortened because alice is longer than name so there would
be no point in shortening it. However speed is kept long enough
to avoid ambiguity with special.
When setting wrap=3 for the name column we obtain the following:
info | metrics
nam i | spee accur specia
bob 4 | 200 21.0% 10.0%
ali 2 | 67 45.0% 456.0%
ce |
It is possible to customize the column separators by passing
separators to the treetable function. Its default value is
[' ', ' | ', ' || '].
Colors
It is possible to use ANSI color codes by passing a list of color codes to treetable() with the colors argument.
The i-th line (including headers) will have the color colors[i % len(colors]. For instance:
treetable(lines, mytable, colors=["30", "39"])
For a good reference on ANSI color codes, checkout this stackoverflow question.
License
treetable is distributed under the Unlicense license.
See the LICENSE file for more information.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file treetable-0.2.6.tar.gz.
File metadata
- Download URL: treetable-0.2.6.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e1d62dbce503fbf24561aee1461b8fbcc2c232ff45661c3b9d0c2081c795bdf
|
|
| MD5 |
92f9e0c9829a41ba8d7d4293c3b4f024
|
|
| BLAKE2b-256 |
8df1e5f28b2485d8d3169ff0167e3e560fa912a96e71916bf11365c9e40f11f5
|
File details
Details for the file treetable-0.2.6-py3-none-any.whl.
File metadata
- Download URL: treetable-0.2.6-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa7dfa0297d2bbc5882191edd2e15f79a5e883e352f489e2acadb221db565adf
|
|
| MD5 |
c67c5cca17af80658a0c427b80f81526
|
|
| BLAKE2b-256 |
e416dd00f9fc5b84cb3fb396d62245e11761accfd9d27fd56ce0024bdd38a0ae
|