donb-hf commited on
Commit
b4b8a6a
Β·
verified Β·
1 Parent(s): 78ed8a7

update graph to json

Browse files
Files changed (1) hide show
  1. app.py +31 -28
app.py CHANGED
@@ -101,36 +101,39 @@ def clear_graph():
101
 
102
  import json
103
 
 
 
104
  def graph_to_json():
105
- # Convert the lesson_graph to a dictionary format
106
- graph_data = {
107
- "nodes": [
108
- {
109
- "id": node,
110
- "type": lesson_graph.nodes[node]["type"],
111
- "description": lesson_graph.nodes[node].get("description", "")
112
- }
113
- for node in lesson_graph.nodes()
114
- ],
115
- "edges": [
116
- {
117
- "source": u,
 
118
  "target": v,
119
- "relationship": lesson_graph.edges[u, v]["relationship"]
120
- }
121
- for u, v in lesson_graph.edges()
122
- ]
123
- }
124
-
125
- # Convert dictionary to JSON string
126
- json_data = json.dumps(graph_data, indent=4)
127
-
128
- # Save to a file
129
- file_name = "/mnt/data/lesson_graph.json"
130
- with open(file_name, "w") as f:
131
- f.write(json_data)
132
-
133
- return file_name
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()