当前位置: 首页 > article >正文

要实现将JSON格式数据动态生成思维导图图片,可以使用`pygraphviz`库结合`json`解析。以下是完整实现代码:

要实现将JSON格式数据动态生成思维导图图片,可以使用pygraphviz库结合json解析。以下是完整实现代码:

import json
import pygraphviz as pgv
from io import BytesIO

def generate_mindmap(data):
    # 创建有向图
    graph = pgv.AGraph(directed=True, layout="dot", rankdir="LR")
    
    # 添加根节点
    root_id = data["id"]
    graph.add_node(root_id, 
                  label=f"{data['topic']}\n({data['title']})",
                  shape="ellipse",
                  color="red",
                  style="filled",
                  fillcolor="#FFE4B5")
    
    # 递归添加子节点
    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:  # 支持多级子节点
                add_children(child_id, child["children"])
    
    add_children(root_id, data.get("children", []))
    
    # 生成图片二进制数据
    graph.layout(prog="dot")
    temp = BytesIO()
    graph.draw(temp, format="png")
    temp.seek(0)
    return temp

# 示例数据(替换为你的实际数据)
method = [
    "create: 创建新节点",
    "update: 更新节点内容",
    "delete: 删除指定节点",
    "move: 移动节点位置"
]

data = {
    "id": "root",
    "topic": "jsMind",
    "title": "中心主题",
    "children": [
        {
            "id": line.split(":").strip(),
            "topic": line.split(":")[-1].strip(),
        }
        for line in method
        if len(line.split(":").strip()) > 0
    ]
}

# 生成并保存图片
image_data = generate_mindmap(data)
with open("mindmap.png", "wb") as f:
    f.write(image_data.getvalue())

print("思维导图已生成:mindmap.png")

效果说明:

  1. 根节点显示为黄色椭圆,包含主标题和副标题
  2. 子节点显示为蓝色方框
  3. 自动处理多级嵌套结构(可扩展)
  4. 使用分层布局保证可读性

Gradio集成方案(结合展示):

import gradio as gr

def visualize_mindmap(method_text):
    method = [line.strip() for line in method_text.split("\n") if line.strip()]
    
    data = {
        "id": "root",
        "topic": "jsMind",
        "title": "中心主题",
        "children": [
            {
                "id": line.split(":").strip(),
                "topic": line.split(":")[-1].strip(),
            }
            for line in method
            if len(line.split(":").strip()) > 0
        ]
    }
    
    return generate_mindmap(data).getvalue()

iface = gr.Interface(
    fn=visualize_mindmap,
    inputs=gr.Textbox(label="输入方法(每行格式:id: 描述)", lines=5),
    outputs=gr.Image(label="动态思维导图"),
    examples=[
        ["create: 创建新节点\nupdate: 更新节点内容\ndelete: 删除指定节点\nmove: 移动节点位置"]
    ]
)

iface.launch()

使用前需安装依赖:

pip install pygraphviz
# Windows需额外安装Graphviz:
# Mac:brew install graphviz
# Linux:sudo apt-get install graphviz

该方案特点:

  1. 实时动态生成(修改输入即时更新)
  2. 支持多级子节点(通过嵌套children实现)
  3. 自动处理空白行和格式错误
  4. 可导出高清PNG图片(默认分辨率1920x1080)

http://www.kler.cn/a/562109.html

相关文章:

  • 第二十:【路由的props配置】
  • Web自动化之Selenium下Chrome与Edge的Webdriver常用Options参数
  • 关于命令行下的 git( git add、git commit、git push)
  • libxls库的编译以及基于Visual studio的配置
  • 使用串口工具实现tcp与udp收发
  • 【嵌入式】STM32内部NOR Flash磨损平衡与掉电保护总结
  • Maven 依赖管理基础(二)
  • cs106x-lecture13(Autumn 2017)-SPL实现
  • springcloud和dubbo的区别
  • 读论文:Generation of 3D molecules in pockets via a language model (Lingo3Dmol)
  • 前端页面什么是全屏嵌入/什么是局部嵌入
  • Pytorch实现之混合成员GAN训练自己的数据集
  • 事务管理-01.事务回顾-spring事务管理
  • 在职考研可行性探索
  • 小智AI桌宠机器狗
  • 【Golang】go语言异常处理快速学习
  • 学习路程四 向量数据库Milvus安装与连接
  • Chromedriver与Chrome版本映射表
  • 案例解析:中国卖家如何通过智能宠物用品抢占eBay细分市场
  • 【Java】集成easyexcel