home » zplus/dokk.org.git
ID: 712e478145b7e6059cec5a320356088fe274c0ee
45 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'
edge_properties = [ 'comics_cartoon', 'comics_author', 'ttrpg_character',
                    'community_project' ]

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 edge_properties:
            if p not in n.keys():
                continue
            
            if not type(n[p]) == list:
                n[p] = [ n[p] ]
            
            for target in n[p]:
                edges.append({
                    'title': p,
                    '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)))