Spaces:
Running
Running
Update network_graph_generator.py
Browse files- network_graph_generator.py +0 -165
network_graph_generator.py
CHANGED
@@ -1,168 +1,3 @@
|
|
1 |
-
# import graphviz
|
2 |
-
# import json
|
3 |
-
# from tempfile import NamedTemporaryFile
|
4 |
-
# import os
|
5 |
-
|
6 |
-
# def generate_network_graph(json_input: str, output_format: str) -> str:
|
7 |
-
# """
|
8 |
-
# Generates a network graph from JSON input.
|
9 |
-
|
10 |
-
# Args:
|
11 |
-
# json_input (str): A JSON string describing the network graph structure.
|
12 |
-
# It must follow the Expected JSON Format Example below.
|
13 |
-
|
14 |
-
# Expected JSON Format Example:
|
15 |
-
# {
|
16 |
-
# "nodes": [
|
17 |
-
# {
|
18 |
-
# "id": "server1",
|
19 |
-
# "label": "Web Server",
|
20 |
-
# "type": "server"
|
21 |
-
# },
|
22 |
-
# {
|
23 |
-
# "id": "db1",
|
24 |
-
# "label": "Database",
|
25 |
-
# "type": "database"
|
26 |
-
# },
|
27 |
-
# {
|
28 |
-
# "id": "user1",
|
29 |
-
# "label": "User",
|
30 |
-
# "type": "user"
|
31 |
-
# },
|
32 |
-
# {
|
33 |
-
# "id": "service1",
|
34 |
-
# "label": "API Service",
|
35 |
-
# "type": "service"
|
36 |
-
# }
|
37 |
-
# ],
|
38 |
-
# "connections": [
|
39 |
-
# {
|
40 |
-
# "from": "user1",
|
41 |
-
# "to": "server1",
|
42 |
-
# "label": "HTTP Request",
|
43 |
-
# "weight": 2
|
44 |
-
# },
|
45 |
-
# {
|
46 |
-
# "from": "server1",
|
47 |
-
# "to": "service1",
|
48 |
-
# "label": "API Call",
|
49 |
-
# "weight": 3
|
50 |
-
# },
|
51 |
-
# {
|
52 |
-
# "from": "service1",
|
53 |
-
# "to": "db1",
|
54 |
-
# "label": "Query",
|
55 |
-
# "weight": 1
|
56 |
-
# }
|
57 |
-
# ]
|
58 |
-
# }
|
59 |
-
|
60 |
-
# Returns:
|
61 |
-
# str: The filepath to the generated PNG image file.
|
62 |
-
# """
|
63 |
-
# try:
|
64 |
-
# if not json_input.strip():
|
65 |
-
# return "Error: Empty input"
|
66 |
-
|
67 |
-
# data = json.loads(json_input)
|
68 |
-
|
69 |
-
# if 'nodes' not in data or 'connections' not in data:
|
70 |
-
# raise ValueError("Missing required fields: nodes or connections")
|
71 |
-
|
72 |
-
# dot = graphviz.Graph(
|
73 |
-
# name='NetworkGraph',
|
74 |
-
# format='png',
|
75 |
-
# engine='neato',
|
76 |
-
# graph_attr={
|
77 |
-
# 'overlap': 'false',
|
78 |
-
# 'splines': 'true',
|
79 |
-
# 'bgcolor': 'white',
|
80 |
-
# 'pad': '0.5',
|
81 |
-
# 'layout': 'neato'
|
82 |
-
# },
|
83 |
-
# node_attr={
|
84 |
-
# 'fixedsize': 'false'
|
85 |
-
# }
|
86 |
-
# )
|
87 |
-
|
88 |
-
# type_colors = {
|
89 |
-
# 'server': '#BEBEBE',
|
90 |
-
# 'service': '#B8D4F1',
|
91 |
-
# 'database': '#A8E6CF',
|
92 |
-
# 'user': '#FFF9C4',
|
93 |
-
# 'default': '#BEBEBE'
|
94 |
-
# }
|
95 |
-
|
96 |
-
# nodes = data.get('nodes', [])
|
97 |
-
# connections = data.get('connections', [])
|
98 |
-
|
99 |
-
# for node in nodes:
|
100 |
-
# node_id = node.get('id')
|
101 |
-
# label = node.get('label')
|
102 |
-
# node_type = node.get('type', 'default')
|
103 |
-
|
104 |
-
# if not all([node_id, label]):
|
105 |
-
# raise ValueError(f"Invalid node: {node}")
|
106 |
-
|
107 |
-
# node_color = type_colors.get(node_type, type_colors['default'])
|
108 |
-
# font_color = 'black'
|
109 |
-
|
110 |
-
# if node_type == 'server':
|
111 |
-
# shape = 'box'
|
112 |
-
# style = 'filled,rounded'
|
113 |
-
# elif node_type == 'database':
|
114 |
-
# shape = 'cylinder'
|
115 |
-
# style = 'filled,rounded'
|
116 |
-
# elif node_type == 'user':
|
117 |
-
# shape = 'ellipse'
|
118 |
-
# style = 'filled,rounded'
|
119 |
-
# elif node_type == 'service':
|
120 |
-
# shape = 'hexagon'
|
121 |
-
# style = 'filled,rounded'
|
122 |
-
# else:
|
123 |
-
# shape = 'circle'
|
124 |
-
# style = 'filled,rounded'
|
125 |
-
|
126 |
-
# dot.node(
|
127 |
-
# node_id,
|
128 |
-
# label,
|
129 |
-
# shape=shape,
|
130 |
-
# style=style,
|
131 |
-
# fillcolor=node_color,
|
132 |
-
# fontcolor=font_color,
|
133 |
-
# fontsize='12'
|
134 |
-
# )
|
135 |
-
|
136 |
-
# for connection in connections:
|
137 |
-
# from_node = connection.get('from')
|
138 |
-
# to_node = connection.get('to')
|
139 |
-
# label = connection.get('label', '')
|
140 |
-
# weight = connection.get('weight', 1)
|
141 |
-
|
142 |
-
# if not all([from_node, to_node]):
|
143 |
-
# raise ValueError(f"Invalid connection: {connection}")
|
144 |
-
|
145 |
-
# penwidth = str(max(1, min(5, weight)))
|
146 |
-
|
147 |
-
# dot.edge(
|
148 |
-
# from_node,
|
149 |
-
# to_node,
|
150 |
-
# label=label,
|
151 |
-
# color='#4a4a4a',
|
152 |
-
# fontcolor='#4a4a4a',
|
153 |
-
# fontsize='10',
|
154 |
-
# penwidth=penwidth
|
155 |
-
# )
|
156 |
-
|
157 |
-
# with NamedTemporaryFile(delete=False, suffix=f'.{output_format}') as tmp:
|
158 |
-
# dot.render(tmp.name, format=output_format, cleanup=True)
|
159 |
-
# return f"{tmp.name}.{output_format}"
|
160 |
-
|
161 |
-
# except json.JSONDecodeError:
|
162 |
-
# return "Error: Invalid JSON format"
|
163 |
-
# except Exception as e:
|
164 |
-
# return f"Error: {str(e)}"
|
165 |
-
|
166 |
import graphviz
|
167 |
import json
|
168 |
from tempfile import NamedTemporaryFile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import graphviz
|
2 |
import json
|
3 |
from tempfile import NamedTemporaryFile
|