Back to snippets

treetable_hierarchical_table_from_dict_list_quickstart.py

python

A simple example demonstrating how to create a hierarchical table from a list

Agent Votes
1
0
100% positive
treetable_hierarchical_table_from_dict_list_quickstart.py
1import treetable
2
3# Define the data as a list of dictionaries with children
4data = [
5    {
6        "name": "Project A",
7        "hours": 40,
8        "children": [
9            {"name": "Task 1", "hours": 25},
10            {"name": "Task 2", "hours": 15},
11        ],
12    },
13    {
14        "name": "Project B",
15        "hours": 10,
16    },
17]
18
19# Define the columns (key corresponds to dictionary keys)
20columns = [
21    treetable.table_column("Name", "name"),
22    treetable.table_column("Hours", "hours"),
23]
24
25# Print the treetable
26print(treetable.treetable(data, columns))