-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtree_handlers.py
More file actions
76 lines (66 loc) · 2.5 KB
/
tree_handlers.py
File metadata and controls
76 lines (66 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import json
from gradio import update
from tree_editor import editor, default_data
import copy
def persist_tree():
"""将当前树数据写回前端专用树快照,不污染后端 canonical JSON。"""
os.makedirs("cache", exist_ok=True)
try:
with open("cache/temp.ui.tree.json", "w", encoding="utf-8") as f:
json.dump(editor.data, f, ensure_ascii=False, indent=2)
except Exception as e:
print(f"[WARN] 写入 JSON 失败: {e}")
def refresh_ui():
choices = editor.get_node_list()
return (
editor.render_html(), # graph
update(choices=choices, value=None), # rename_sel
update(choices=choices, value=None), # add_parent_sel
update(choices=choices, value=None), # del_sel
"操作成功,视图已更新",
update(value=""), # rename_inp clear
update(value=""), # add_name_inp clear
)
def handle_rename(target, new_name):
if not target or not new_name:
return (
editor.render_html(),
update(choices=editor.get_node_list(), value=None),
update(choices=editor.get_node_list(), value=None),
update(choices=editor.get_node_list(), value=None),
"请输入节点和新名称",
update(value=""),
update(value=""),
)
editor.rename_node(target, new_name)
persist_tree()
return refresh_ui()
def handle_add(parent, child_name):
if not parent or not child_name:
return (
editor.render_html(),
update(choices=editor.get_node_list(), value=None),
update(choices=editor.get_node_list(), value=None),
update(choices=editor.get_node_list(), value=None),
"请选择父节点并输入新名称",
update(value=""),
update(value=""),
)
editor.add_child(parent, child_name)
persist_tree()
return refresh_ui()
def handle_delete(target):
if not target:
return (
editor.render_html(),
update(choices=editor.get_node_list(), value=None),
update(choices=editor.get_node_list(), value=None),
update(choices=editor.get_node_list(), value=None),
"请选择要删除的节点",
update(value=""),
update(value=""),
)
editor.delete_node(target)
persist_tree()
return refresh_ui()