Treetable

0.2.6 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This example demonstrates how to define a nested table structure using `table`, `group`, and `leaf` functions, then render data using the main `treetable` function. It showcases custom alignment and formatting for different data fields.

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))

view raw JSON →