update graph to json
Browse files
app.py
CHANGED
@@ -101,36 +101,39 @@ def clear_graph():
|
|
101 |
|
102 |
import json
|
103 |
|
|
|
|
|
104 |
def graph_to_json():
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
118 |
"target": v,
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
|
135 |
# Gradio interface
|
136 |
demo = gr.Blocks()
|
|
|
101 |
|
102 |
import json
|
103 |
|
104 |
+
import json
|
105 |
+
|
106 |
def graph_to_json():
|
107 |
+
try:
|
108 |
+
# Convert the lesson_graph to a dictionary format
|
109 |
+
graph_data = {
|
110 |
+
"nodes": [
|
111 |
+
{
|
112 |
+
"id": node,
|
113 |
+
"type": lesson_graph.nodes[node]["type"],
|
114 |
+
"description": lesson_graph.nodes[node].get("description", "")
|
115 |
+
}
|
116 |
+
for node in lesson_graph.nodes()
|
117 |
+
],
|
118 |
+
"edges": [
|
119 |
+
{
|
120 |
+
"source": u,
|
121 |
"target": v,
|
122 |
+
"relationship": lesson_graph.edges[u, v]["relationship"]
|
123 |
+
}
|
124 |
+
for u, v in lesson_graph.edges()
|
125 |
+
]
|
126 |
+
}
|
127 |
+
|
128 |
+
# Convert dictionary to JSON string
|
129 |
+
json_data = json.dumps(graph_data, indent=4)
|
130 |
+
|
131 |
+
# Instead of writing to a file, return the JSON data as a message
|
132 |
+
return f"Graph Data in JSON Format:\n{json_data}"
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
# If any error occurs, catch it and display it in the message_output
|
136 |
+
return f"An error occurred: {str(e)}"
|
137 |
|
138 |
# Gradio interface
|
139 |
demo = gr.Blocks()
|