To extend your interactive graph editor with additional features like cleaning the…
- User
To extend your interactive graph editor with additional features like cleaning the dataset, attaching metadata, saving images, and integrating Wokwi simulator circuit diagrams, here's an updated approach:
Key Features:
- Attach Metadata: Allows users to input project metadata like prompt, title, code, and description.
- Save Graph Image: Saves the circuit visualization as an image (PNG format).
- Dataset Cleaning: Removes redundant or invalid nodes, edges, and isolated components.
- Wokwi API Integration: Automatically generate the circuit diagram based on the Wokwi JSON format, allowing users to interact with hardware components virtually.
Extended Graph Editor
1. Handling Wokwi Circuit JSON Format:
Using the Wokwi API, you can extend the editor to visualize circuit diagrams by reading Wokwi JSON files and constructing the corresponding graph in your editor.
import tkinter as tk from tkinter import simpledialog, messagebox, filedialog import networkx as nx import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg import json import os class GraphEditor: def __init__(self, root): self.root = root self.root.title("Graph Editor with Wokwi Integration") # Initialize a graph self.graph = nx.Graph() # Initialize metadata self.metadata = { "title": "", "prompt": "", "code": "", "description": "" } # Create UI elements self.create_widgets() self.update_graph_display() def create_widgets(self): # Frame for graph visualization self.frame = tk.Frame(self.root) self.frame.pack(side=tk.LEFT) # Graph canvas using Matplotlib self.fig, self.ax = plt.subplots(figsize=(6, 6)) self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame) self.canvas.get_tk_widget().pack() # Control buttons control_frame = tk.Frame(self.root) control_frame.pack(side=tk.RIGHT) tk.Button(control_frame, text="Add Node", command=self.add_node).pack(fill=tk.X) tk.Button(control_frame, text="Add Edge", command=self.add_edge).pack(fill=tk.X) tk.Button(control_frame, text="Enter Metadata", command=self.enter_metadata).pack(fill=tk.X) tk.Button(control_frame, text="Save Circuit Image", command=self.save_graph_image).pack(fill=tk.X) tk.Button(control_frame, text="Save Project", command=self.save_project).pack(fill=tk.X) tk.Button(control_frame, text="Load Project", command=self.load_project).pack(fill=tk.X) tk.Button(control_frame, text="Clean Dataset", command=self.clean_dataset).pack(fill=tk.X) tk.Button(control_frame, text="Load Wokwi Circuit", command=self.load_wokwi_circuit).pack(fill=tk.X) tk.Button(control_frame, text="Clear Graph", command=self.clear_graph).pack(fill=tk.X) def update_graph_display(self): """Redraw the graph with the latest nodes and edges.""" self.ax.clear() pos = nx.spring_layout(self.graph) # Recalculate layout for nodes nx.draw(self.graph, pos, with_labels=True, ax=self.ax, node_color="lightblue", font_size=10, font_weight="bold") self.canvas.draw() def add_node(self): node_name = simpledialog.askstring("Input", "Enter node name:") if node_name: self.graph.add_node(node_name) self.update_graph_display() def add_edge(self): node1 = simpledialog.askstring("Input", "Enter first node name:") node2 = simpledialog.askstring("Input", "Enter second node name:") if node1 and node2 and node1 in self.graph.nodes and node2 in self.graph.nodes: self.graph.add_edge(node1, node2) self.update_graph_display() else: messagebox.showerror("Error", "Invalid node names!") def enter_metadata(self): """Prompts the user to enter metadata (title, prompt, code, and description).""" self.metadata["title"] = simpledialog.askstring("Input", "Enter project title:") self.metadata["prompt"] = simpledialog.askstring("Input", "Enter graph prompt:") self.metadata["code"] = simpledialog.askstring("Input", "Enter project code:") self.metadata["description"] = simpledialog.askstring("Input", "Enter project description:") messagebox.showinfo("Info", "Metadata saved successfully!") def save_graph_image(self): """Saves the current graph as an image (circuit diagram).""" file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")]) if file_path: self.fig.savefig(file_path) messagebox.showinfo("Saved", f"Circuit image saved as {file_path}") def save_project(self): """Saves the graph, metadata, and image into a JSON file.""" file_path = filedialog.asksaveasfilename(defaultextension=".json", filetypes=[("JSON files", "*.json")]) if file_path: graph_data = nx.node_link_data(self.graph) # Convert graph to node-link format project_data = { "graph": graph_data, "metadata": self.metadata } with open(file_path, 'w') as f: json.dump(project_data, f, indent=4) messagebox.showinfo("Saved", f"Project saved successfully as {file_path}") def load_project(self): """Loads a project (graph, metadata, and image) from a JSON file.""" file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")]) if file_path: with open(file_path, 'r') as f: project_data = json.load(f) self.graph = nx.node_link_graph(project_data["graph"]) # Load graph from node-link format self.metadata = project_data["metadata"] # Load metadata self.update_graph_display() messagebox.showinfo("Loaded", "Project loaded successfully!") def load_wokwi_circuit(self): """Load a Wokwi circuit JSON file and construct the corresponding graph.""" file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")]) if file_path: with open(file_path, 'r') as f: wokwi_data = json.load(f) self.build_wokwi_graph(wokwi_data) def build_wokwi_graph(self, wokwi_data): """Construct the graph from the Wokwi JSON circuit data.""" self.clear_graph() components = wokwi_data.get("parts", []) connections = wokwi_data.get("connections", []) # Add components as nodes for component in components: comp_id = component["id"] self.graph.add_node(comp_id, type=component["type"]) # Add connections as edges for connection in connections: source, target, *_ = connection self.graph.add_edge(source, target) self.update_graph_display() def clean_dataset(self): """Cleans the dataset by removing isolated nodes and self-loops.""" isolated_nodes = list(nx.isolates(self.graph)) # Find isolated nodes if isolated_nodes: self.graph.remove_nodes_from(isolated_nodes) messagebox.showinfo("Cleaned", f"Removed isolated nodes: {isolated_nodes}") self_loops = list(nx.selfloop_edges(self.graph)) # Find self-loops if self_loops: self.graph.remove_edges_from(self_loops) messagebox.showinfo("Cleaned", f"Removed self-loops: {self_loops}") self.update_graph_display() def clear_graph(self): """Clears the graph and resets metadata.""" self.graph.clear() self.metadata = { "title": "", "prompt": "", "code": "", "description": "" } self.update_graph_display() messagebox.showinfo("Cleared", "Graph and metadata cleared!") # Create the main window root = tk.Tk() app = GraphEditor(root) root.mainloop() - Assistant