Browse Source

refactor: Enhance structure file saving by converting sets to lists for JSON serialization and ensuring consistent file content for download

zhch158_admin 10 hours ago
parent
commit
57f00e0526
1 changed files with 19 additions and 9 deletions
  1. 19 9
      table_line_generator/editor/save_controls.py

+ 19 - 9
table_line_generator/editor/save_controls.py

@@ -142,18 +142,28 @@ def _save_structure_file(structure, output_dir, base_name, suffix, saved_files):
     """保存结构配置文件"""
     """保存结构配置文件"""
     structure_filename = f"{base_name}{suffix}"
     structure_filename = f"{base_name}{suffix}"
     structure_path = output_dir / structure_filename
     structure_path = output_dir / structure_filename
-    # save_structure_to_config(structure, structure_path)
+    
+    # 准备保存的数据(处理 set 类型等不可序列化对象)
+    save_data = structure.copy()
+    if 'modified_h_lines' in save_data and isinstance(save_data['modified_h_lines'], set):
+        save_data['modified_h_lines'] = list(save_data['modified_h_lines'])
+    if 'modified_v_lines' in save_data and isinstance(save_data['modified_v_lines'], set):
+        save_data['modified_v_lines'] = list(save_data['modified_v_lines'])
+    
     with open(structure_path, 'w', encoding='utf-8') as f:
     with open(structure_path, 'w', encoding='utf-8') as f:
-        json.dump(structure, f, indent=2, ensure_ascii=False)
+        json.dump(save_data, f, indent=2, ensure_ascii=False)
     saved_files.append(("配置文件", structure_path))
     saved_files.append(("配置文件", structure_path))
     
     
-    with open(structure_path, 'r') as f:
-        st.download_button(
-            "📥 下载配置文件",
-            f.read(),
-            file_name=f"{base_name}_structure.json",
-            mime="application/json"
-        )
+    # 重新读取文件内容用于下载按钮(确保内容一致性)
+    with open(structure_path, 'r', encoding='utf-8') as f:
+        file_content = f.read()
+        
+    st.download_button(
+        "📥 下载配置文件",
+        file_content,
+        file_name=f"{base_name}_structure.json",
+        mime="application/json"
+    )
 
 
 
 
 def _save_image_file(image, structure, line_width, color_option, line_colors, 
 def _save_image_file(image, structure, line_width, color_option, line_colors,