ID: 15619afd4ae2a9061961280b3a08593701b11b6e
43 lines
—
1K —
View raw
| #!/usr/bin/env python3
"""
This script creates the data.js file containing the list of nodes end edges for
the vis.js graph. Just place data.js in the same folder with index.html.
"""
import json
from pathlib import Path
data = '../dokk/data'
nodes = []
edges = []
for path in Path(data).glob('**/*.json',):
with open(path, 'r') as f:
n = json.loads(f.read())
nodes.append({
'title': n['node_name'] + ('\n' + n['node_description'] if 'node_description' in n else ''),
#'color': { 'background': '', 'border': ''},
'label': n['node_name'],
'id': n['node_id']
})
for p in n.keys():
if not p.startswith('*'):
continue
if not type(n[p]) == list:
n[p] = [ n[p] ]
for target in n[p]:
edges.append({
'title': p[1:],
'from': n['node_id'],
#'label': p,
'to': target,
})
with open('data.js', 'w') as f:
f.write('var nodes={};\nvar edges={}'.format(json.dumps(nodes), json.dumps(edges)))
|