Treetable
Treetable is a Python helper library designed to pretty-print data in an ASCII table format with a hierarchical, tree-like structure. It enables the creation of complex tables with nested sub-tables, using different separators for each level to enhance readability. The library is currently at version 0.2.6 and appears to be actively maintained.
Warnings
- gotcha Be cautious when searching for 'TreeTable' online, as many results pertain to JavaScript/Angular UI components (e.g., PrimeNG, Reactabular) rather than this specific Python library. Ensure your search context is 'python treetable library' to avoid confusion.
- gotcha The `treetable` function expects data in a specific format: a list of nested dictionaries where keys match the 'key' arguments defined in your `table`, `group`, and `leaf` structure. Mismatched keys or an incorrect data structure will lead to missing data or errors in the output.
- gotcha The library uses default column separators (`[' ', ' | ', ' || ']`). If your table has more than three levels of nesting or you require different visual separation, you must explicitly pass a `separators` argument to the `treetable` function.
- gotcha The `shorten` argument for columns automatically shortens names but will not shorten a display name if it's already shorter than the underlying column's content width, or if shortening would create ambiguity with other columns in the same sub-table. This behavior can be surprising if strict column width control is expected.
Install
-
pip install treetable
Imports
- treetable
from treetable import treetable
- table
from treetable import table
- group
from treetable import group
- leaf
from treetable import leaf
Quickstart
from treetable import table, group, leaf, treetable
mytable = table([
group('info', [
leaf('name'),
leaf('index')
]),
group('metrics', align='>', groups=[
leaf('speed', '.0f'),
leaf('accuracy', '.1%'),
leaf('special', '.1%', align='=')
]),
])
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}},
]
print(treetable(lines, mytable))