mknodes.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python3
  2. """
  3. This script creates the data.js file containing the list of nodes end edges for
  4. the vis.js graph. Just place data.js in the same folder with index.html.
  5. """
  6. import json
  7. from pathlib import Path
  8. data = '../dokk/data'
  9. edge_properties = [ 'comics_cartoon', 'comics_author', 'ttrpg_character',
  10. 'community_project', 'game_genre', 'software_type',
  11. 'node_instance_of', 'node_subclass_of' ]
  12. nodes = []
  13. edges = []
  14. for path in Path(data).glob('**/*.json',):
  15. with open(path, 'r') as f:
  16. n = json.loads(f.read())
  17. nodes.append({
  18. 'title': n['node_name'] + ('\n' + n['node_description'] if 'node_description' in n else ''),
  19. #'color': { 'background': '', 'border': ''},
  20. 'label': n['node_name'],
  21. 'id': n['node_id']
  22. })
  23. for p in edge_properties:
  24. if p not in n.keys():
  25. continue
  26. if not type(n[p]) == list:
  27. n[p] = [ n[p] ]
  28. for target in n[p]:
  29. edges.append({
  30. 'title': p,
  31. 'from': n['node_id'],
  32. #'label': p,
  33. 'to': target,
  34. })
  35. with open('data.js', 'w') as f:
  36. f.write('var nodes={};\nvar edges={}'.format(json.dumps(nodes), json.dumps(edges)))