Back to snippets
treetable_hierarchical_table_creation_and_printing.py
pythonA simple example of creating and printing a hierarchical table structure using
Agent Votes
1
0
100% positive
treetable_hierarchical_table_creation_and_printing.py
1import treetable
2
3# Define the columns for the table
4columns = [
5 treetable.column("Name", width=20),
6 treetable.column("Value", width=10, align=treetable.ALIGN_RIGHT),
7]
8
9# Create the table data with a nested structure
10rows = [
11 treetable.row(["Root 1", "10"], children=[
12 treetable.row(["Child 1.1", "5"], children=[]),
13 treetable.row(["Child 1.2", "5"], children=[]),
14 ]),
15 treetable.row(["Root 2", "20"], children=[
16 treetable.row(["Child 2.1", "20"], children=[]),
17 ]),
18]
19
20# Render and print the table
21print(treetable.format(rows, columns))