def plot_graph(self, G): plt.figure(figsize=(4, 3)) nx.draw(G, with_labels=True, node_color='lightblue', font_weight='bold') plt.axis('off') # Save to BytesIO buffer buf = io.BytesIO() plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0.1) plt.close() # Reset buffer and open as PIL image buf.seek(0) image = Image.open(buf) return image, buf # Return image and buffer for download