Created
December 22, 2022 11:11
-
-
Save whacked/eb7333af9182a6b277d1bd0c69047b12 to your computer and use it in GitHub Desktop.
generate graphs to test obsidian canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import json | |
import random | |
import math | |
if len(sys.argv) >= 3: | |
num_rows = int(sys.argv[1]) | |
num_columns = int(sys.argv[2]) | |
else: | |
num_rows = 20 | |
num_columns = 30 | |
num_nodes = num_columns * num_rows | |
file_name = sys.argv[-1].endswith('.canvas') \ | |
and sys.argv[-1] or f'test-{num_nodes}.canvas' | |
nodes = [] | |
edges = [] | |
node_width = 60 | |
node_height = 30 | |
node_margin = node_width / 1 | |
canvas_width = num_columns * (node_width + node_margin) | |
canvas_height = num_rows * (node_height + node_margin) | |
for i in range(num_nodes): | |
node_id = f'node_{i}' | |
node_text = f'{i}' | |
r = int(255 - i / num_nodes * 256) << 16 | |
g = int(i / num_nodes * 256) << 8 | |
b = int(i / num_nodes * 256) << 0 | |
color_hex = f'#{hex(int(r + g + b))[2:]:0>6}' | |
node = { | |
'id': node_id, | |
'type': 'text', | |
'text': node_text, | |
'x': i % num_columns * (node_width + node_margin) - canvas_width / 2, | |
'y': math.floor(i / num_columns) \ | |
* (node_height + node_margin) - canvas_height / 2, | |
'width': node_width, | |
'height': node_height, | |
'color': color_hex, | |
} | |
nodes.append(node) | |
num_connections = random.randint(1, 1) | |
for target_node_id in set( | |
[] + [ | |
random.choice(nodes[-20:])['id'] | |
for j in range(num_connections) | |
]): | |
sides = ['left', 'right', 'top', 'bottom'] | |
random.shuffle(sides) | |
edges.append({ | |
'id': f'edge_{node_id}_{target_node_id}', | |
'fromNode': node_id, | |
'toNode': target_node_id, | |
'fromSide': sides[0], | |
'toSide': sides[1], | |
}) | |
with open(file_name, 'w') as ofile: | |
ofile.write(json.dumps({'nodes': nodes, 'edges': edges}, indent=2)) | |
print(f'wrote: {ofile.name}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment