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", []))
# 生成圖片二進制數(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é)點顯示為藍色方框
- 自動處理多級嵌套結(jié)構(gòu)(可擴展)
- 使用分層布局保證可讀性
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圖片(默認分辨率1920x1080)
以上就是Python實現(xiàn)JSON數(shù)據(jù)動態(tài)生成思維導(dǎo)圖圖片的詳細內(nèi)容,更多關(guān)于Python數(shù)據(jù)生成思維導(dǎo)圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python開發(fā)之os與os.path的使用小結(jié)
這篇文章主要介紹了Python開發(fā)之os與os.path的使用小結(jié),本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-05-05
jupyter notebook 寫代碼自動補全的實現(xiàn)
這篇文章主要介紹了jupyter notebook 寫代碼自動補全的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Jupyter Notebook運行代碼無反應(yīng)問題及解決方法
這篇文章主要介紹了Jupyter Notebook運行代碼無反應(yīng)問題及解決方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
Python3.9.1中使用split()的處理方法(推薦)
這篇文章主要介紹了Python3.9.1中使用split()的處理方法(推薦),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

