ZahirJS commited on
Commit
8f78956
·
verified ·
1 Parent(s): 1dbebbe

Create radial_diagram_generator.py

Browse files
Files changed (1) hide show
  1. radial_diagram_generator.py +62 -0
radial_diagram_generator.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import graphviz
2
+ import json
3
+ from tempfile import NamedTemporaryFile
4
+ import os
5
+ from graph_generator_utils import add_nodes_and_edges
6
+
7
+ def generate_radial_diagram(json_input: str) -> str:
8
+ """
9
+ Generates a radial (center-expanded) diagram from JSON input.
10
+ """
11
+ try:
12
+ if not json_input.strip():
13
+ return "Error: Empty input"
14
+
15
+ data = json.loads(json_input)
16
+
17
+ if 'central_node' not in data or 'nodes' not in data:
18
+ raise ValueError("Missing required fields: central_node or nodes")
19
+
20
+ dot = graphviz.Digraph(
21
+ name='RadialDiagram',
22
+ format='png',
23
+ engine='neato', # Use 'neato' or 'fdp' for radial/force-directed layout
24
+ graph_attr={
25
+ 'overlap': 'false', # Prevent node overlap
26
+ 'splines': 'true', # Smooth splines for edges
27
+ 'bgcolor': 'white', # White background
28
+ 'pad': '0.5', # Padding around the graph
29
+ 'layout': 'neato' # Explicitly set layout engine for consistency
30
+ },
31
+ node_attr={
32
+ 'fixedsize': 'false' # Allow nodes to resize based on content
33
+ }
34
+ )
35
+
36
+ base_color = '#19191a' # Base color for the central node and gradient
37
+
38
+ # Central node styling (rounded box, dark color)
39
+ dot.node(
40
+ 'central',
41
+ data['central_node'],
42
+ shape='box', # Rectangular shape
43
+ style='filled,rounded', # Filled and rounded corners
44
+ fillcolor=base_color, # Darkest color
45
+ fontcolor='white', # White text for dark background
46
+ fontsize='16' # Larger font for central node
47
+ )
48
+
49
+ # Add child nodes and edges recursively starting from depth 1
50
+ # The add_nodes_and_edges function will handle styling consistent with other graphs
51
+ add_nodes_and_edges(dot, 'central', data.get('nodes', []), current_depth=1, base_color=base_color)
52
+
53
+ # Save to temporary file
54
+ with NamedTemporaryFile(delete=False, suffix='.png') as tmp:
55
+ dot.render(tmp.name, format='png', cleanup=True)
56
+ return tmp.name + '.png'
57
+
58
+ except json.JSONDecodeError:
59
+ return "Error: Invalid JSON format"
60
+ except Exception as e:
61
+ return f"Error: {str(e)}"
62
+