Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,23 @@ def create_graph_from_json_text(json_text):
|
|
| 7 |
json_data = json.loads(json_text)
|
| 8 |
|
| 9 |
graph = Digraph()
|
| 10 |
-
|
| 11 |
-
if isinstance(
|
| 12 |
-
for
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
else:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return graph
|
| 17 |
|
| 18 |
def main():
|
|
|
|
| 7 |
json_data = json.loads(json_text)
|
| 8 |
|
| 9 |
graph = Digraph()
|
| 10 |
+
def add_nodes_and_edges(data, parent_key=None):
|
| 11 |
+
if isinstance(data, dict):
|
| 12 |
+
for key, value in data.items():
|
| 13 |
+
if parent_key is not None:
|
| 14 |
+
graph.edge(str(parent_key), str(key))
|
| 15 |
+
add_nodes_and_edges(value, key)
|
| 16 |
+
elif isinstance(data, list):
|
| 17 |
+
for i, item in enumerate(data):
|
| 18 |
+
if parent_key is not None:
|
| 19 |
+
graph.edge(str(parent_key), f"{parent_key}_{i}")
|
| 20 |
+
add_nodes_and_edges(item, f"{parent_key}_{i}")
|
| 21 |
else:
|
| 22 |
+
if parent_key is not None:
|
| 23 |
+
graph.edge(str(parent_key), str(data))
|
| 24 |
+
|
| 25 |
+
add_nodes_and_edges(json_data)
|
| 26 |
+
|
| 27 |
return graph
|
| 28 |
|
| 29 |
def main():
|