Hi I am trying to represent a Modified-Preorder-Tree-Traversal as a Python structure I can then output to json as my current goal is to display the tree in jstree
Lets say I have a table as shown http://imrannazar.com/Modified-Preorder-Tree-Traversal (Each row also has a parent_id in my case) like so
Node ID Name Left MPTT value Right MPTT value ParentID
1 (Root) 1 16 -1
2 Articles 2 11 1
5 Fiction 3 8 2
7 Fantasy 4 5 5
8 Sci-fi 6 7 5
6 Reference 9 10 2
3 Portfolio 12 13 1
4 Contact 14 15 1
The Json format for jstree is like
[
{
"data" : "Root",
"children" : [
{
"data":"Articles",
"children : [
{"data":"Fiction"},
{"data":"Reference"}
]
},
{"data":"Portfolio"},
{"data":"Contact"}]
},
]
How can I convert the above table to a Python format to output this json.
I thought about somehow using a nested dictionary as below
class NestedDict(dict):
def __missing__(self, key):
return self.setdefault(key, NestedDict())
Not sure of the algorithm I need though.
Any help greatly appreciated.
Thanks
You should really try to do it yourself, show what you've done and where it doesn't work. But I had a few minutes free, so...
To parse the table, you can use the csv
module. I'll leave that to you.
Probably not an optimal solution, but this will do it:
datain = (
(1,'Root',1,16,-1),
(2,'Articles',2,11,1),
(5,'Fiction',3,8,2),
(7,'Fantasy',4,5,5),
(8,'Sci-fi',6,7,5),
(6,'Reference',9,10,2),
(3,'Portfolio',12,13,1),
(4,'Contact',14,15,1),
)
def convert_to_json(data):
node_index = dict()
parent_index = dict()
for node in data:
node_index[node[0]] = node
parent_index.setdefault(node[4],[]).append(node)
def process_node(index):
result = { 'data' : node_index[index][1] }
for node in parent_index.get(index,[]):
result.setdefault('children',[]).append(process_node(node[0]))
return result
node = process_node(1)
return [node]
Returns:
[
{
'data': 'Root',
'children': [
{
'data': 'Articles',
'children': [
{
'data': 'Fiction',
'children': [
{ 'data': 'Fantasy' },
{ 'data': 'Sci-fi' }
]
},
{ 'data': 'Reference' }
]
},
{ 'data': 'Portfolio' },
{ 'data': 'Contact' }
]
}
]