re-add draw graph
Browse files- lesson_graph.py +35 -0
lesson_graph.py
CHANGED
@@ -155,6 +155,41 @@ class LessonGraph:
|
|
155 |
|
156 |
return search_string, image
|
157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
def _add_legend(self, ax):
|
159 |
legend_elements = [mpatches.Patch(color=color, label=node_type.value) for node_type, color in self.COLOR_MAP.items()]
|
160 |
ax.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1, 1), title="Node Types")
|
|
|
155 |
|
156 |
return search_string, image
|
157 |
|
158 |
+
def draw_graph(self) -> Image.Image:
|
159 |
+
"""
|
160 |
+
Visualize the graph using Matplotlib, handling layout, labels, and interactivity.
|
161 |
+
"""
|
162 |
+
fig, ax = plt.subplots(figsize=(14, 10))
|
163 |
+
pos = nx.spring_layout(self.graph, k=0.9, iterations=50)
|
164 |
+
|
165 |
+
self._draw_nodes(ax, pos)
|
166 |
+
self._draw_edges(ax, pos)
|
167 |
+
self._add_legend(ax)
|
168 |
+
|
169 |
+
plt.title("Your Educational Landscape", fontsize=16)
|
170 |
+
plt.axis('off')
|
171 |
+
plt.tight_layout()
|
172 |
+
|
173 |
+
self._add_interactivity()
|
174 |
+
|
175 |
+
# Save the plot to a BytesIO object
|
176 |
+
buf = BytesIO()
|
177 |
+
plt.savefig(buf, format="png", dpi=300, bbox_inches="tight")
|
178 |
+
buf.seek(0)
|
179 |
+
plt.close(fig)
|
180 |
+
|
181 |
+
return Image.open(buf)
|
182 |
+
|
183 |
+
def _draw_nodes(self, ax, pos):
|
184 |
+
node_colors = [self.COLOR_MAP[self.graph.nodes[node]['type']] for node in self.graph.nodes()]
|
185 |
+
nx.draw_networkx_nodes(self.graph, pos, node_color=node_colors, node_size=3000, alpha=0.8, ax=ax)
|
186 |
+
nx.draw_networkx_labels(self.graph, pos, font_size=10, font_weight="bold", ax=ax)
|
187 |
+
|
188 |
+
def _draw_edges(self, ax, pos):
|
189 |
+
nx.draw_networkx_edges(self.graph, pos, edge_color='gray', arrows=True, arrowsize=20, ax=ax)
|
190 |
+
edge_labels = nx.get_edge_attributes(self.graph, 'relationship')
|
191 |
+
nx.draw_networkx_edge_labels(self.graph, pos, edge_labels=edge_labels, font_size=8, ax=ax)
|
192 |
+
|
193 |
def _add_legend(self, ax):
|
194 |
legend_elements = [mpatches.Patch(color=color, label=node_type.value) for node_type, color in self.COLOR_MAP.items()]
|
195 |
ax.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1, 1), title="Node Types")
|