To dynamically generate mind map images from JSON format data, you can use the pygraphviz library to combine with `json` analysis. The following is the complete implementation code:
import json import pygraphviz as pgv from io import BytesIO def generate_mindmap(data): # Create a directed graph graph = (directed=True, layout="dot", rankdir="LR") # Add root node root_id = data["id"] graph.add_node(root_id, label=f"{data['topic']}\n({data['title']})", shape="ellipse", color="red", style="filled", fillcolor="#FFE4B5") # Add child nodes recursively def add_children(parent_id, children): for child in children: child_id = child["id"] graph.add_node(child_id, label=child["topic"], shape="box", color="blue") graph.add_edge(parent_id, child_id) if "children" in child: # Support multi-level child nodes add_children(child_id, child["children"]) add_children(root_id, ("children", [])) # Generate image binary data (prog="dot") temp = BytesIO() (temp, format="png") (0) return temp # Sample data (replace with your actual data)method = [ "create: Create a new node", "update: Update node content", "delete: Delete the specified node", "move: mobile node location" ] data = { "id": "root", "topic": "jsMind", "title": "Central Theme", "children": [ { "id": (":").strip(), "topic": (":")[-1].strip(), } for line in method if len((":").strip()) > 0 ] } # Generate and save the pictureimage_data = generate_mindmap(data) with open("", "wb") as f: (image_data.getvalue()) print("Mind Map Generated:")
Effect description:
- The root node appears as a yellow ellipse with the main title and the subtitle
- The child nodes are displayed as blue boxes
- Automatically handle multi-level nested structures (scalable)
- Use hierarchical layout to ensure readability
Gradio integration solution (in combination):
import gradio as gr def visualize_mindmap(method_text): method = [() for line in method_text.split("\n") if ()] data = { "id": "root", "topic": "jsMind", "title": "Central Theme", "children": [ { "id": (":").strip(), "topic": (":")[-1].strip(), } for line in method if len((":").strip()) > 0 ] } return generate_mindmap(data).getvalue() iface = ( fn=visualize_mindmap, inputs=(label="Input method (Format per line: id: Description)", lines=5), outputs=(label="Dynamic Mind Map"), examples=[ ["create: Create a new node\nupdate: Update node content\ndelete: Delete the specified node\nmove: Move node location"] ] ) ()
Dependencies need to be installed before use:
pip install pygraphviz # Windows requires additional installation of Graphviz:# Mac:brew install graphviz # Linux:sudo apt-get install graphviz
Features of this solution:
- Real-time dynamic generation (modify input and update instantly)
- Supports multi-level child nodes (implemented through nested children)
- Automatically handle blank lines and format errors
- High-definition PNG pictures can be exported (default resolution 1920x1080)
The above is the detailed content of Python implementing JSON data dynamically generates mind map pictures. For more information about Python data generation mind maps, please pay attention to my other related articles!