Python實現(xiàn)JSON數(shù)據(jù)動態(tài)生成思維導(dǎo)圖圖片
要實現(xiàn)將JSON格式數(shù)據(jù)動態(tài)生成思維導(dǎo)圖圖片,可以使用`pygraphviz`庫結(jié)合`json`解析。以下是完整實現(xiàn)代碼:
import json import pygraphviz as pgv from io import BytesIO def generate_mindmap(data): # 創(chuàng)建有向圖 graph = pgv.AGraph(directed=True, layout="dot", rankdir="LR") # 添加根節(jié)點 root_id = data["id"] graph.add_node(root_id, label=f"{data['topic']}\n({data['title']})", shape="ellipse", color="red", style="filled", fillcolor="#FFE4B5") # 遞歸添加子節(jié)點 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: # 支持多級子節(jié)點 add_children(child_id, child["children"]) add_children(root_id, data.get("children", [])) # 生成圖片二進(jìn)制數(shù)據(jù) graph.layout(prog="dot") temp = BytesIO() graph.draw(temp, format="png") temp.seek(0) return temp # 示例數(shù)據(jù)(替換為你的實際數(shù)據(jù)) method = [ "create: 創(chuàng)建新節(jié)點", "update: 更新節(jié)點內(nèi)容", "delete: 刪除指定節(jié)點", "move: 移動節(jié)點位置" ] 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("思維導(dǎo)圖已生成:mindmap.png")
效果說明:
- 根節(jié)點顯示為黃色橢圓,包含主標(biāo)題和副標(biāo)題
- 子節(jié)點顯示為藍(lán)色方框
- 自動處理多級嵌套結(jié)構(gòu)(可擴(kuò)展)
- 使用分層布局保證可讀性
Gradio集成方案(結(jié)合展示):
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="動態(tài)思維導(dǎo)圖"), examples=[ ["create: 創(chuàng)建新節(jié)點\nupdate: 更新節(jié)點內(nèi)容\ndelete: 刪除指定節(jié)點\nmove: 移動節(jié)點位置"] ] ) iface.launch()
使用前需安裝依賴:
pip install pygraphviz # Windows需額外安裝Graphviz: # Mac:brew install graphviz # Linux:sudo apt-get install graphviz
該方案特點:
- 實時動態(tài)生成(修改輸入即時更新)
- 支持多級子節(jié)點(通過嵌套children實現(xiàn))
- 自動處理空白行和格式錯誤
- 可導(dǎo)出高清PNG圖片(默認(rèn)分辨率1920x1080)
以上就是Python實現(xiàn)JSON數(shù)據(jù)動態(tài)生成思維導(dǎo)圖圖片的詳細(xì)內(nèi)容,更多關(guān)于Python數(shù)據(jù)生成思維導(dǎo)圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python機(jī)器學(xué)習(xí)NLP自然語言處理基本操作新聞分類
本文是Python機(jī)器學(xué)習(xí)NLP自然語言處理系列文章,開始我們自然語言處理 (NLP) 的學(xué)習(xí)旅程. 本文主要學(xué)習(xí)NLP自然語言處理基本操作新聞分類2021-09-09Win7下搭建python開發(fā)環(huán)境圖文教程(安裝Python、pip、解釋器)
這篇文章主要為大家分享了Win7下搭建python開發(fā)環(huán)境圖文教程,本文主要介紹了安裝Python、pip、解釋器的詳細(xì)步驟,感興趣的小伙伴們可以參考一下2016-05-05matplotlib 范圍選區(qū)(SpanSelector)的使用
這篇文章主要介紹了matplotlib 范圍選區(qū)(SpanSelector)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02