analysis_controls.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """
  2. 表格结构分析控件
  3. """
  4. import streamlit as st
  5. from .drawing import clear_table_image_cache
  6. def create_analysis_section(y_tolerance: int, x_tolerance: int, min_row_height: int):
  7. """
  8. 创建分析区域
  9. Args:
  10. y_tolerance: Y轴聚类容差
  11. x_tolerance: X轴聚类容差
  12. min_row_height: 最小行高
  13. """
  14. if st.button("🔍 分析表格结构"):
  15. with st.spinner("分析中..."):
  16. try:
  17. generator = st.session_state.generator
  18. structure = generator.analyze_table_structure(
  19. y_tolerance=y_tolerance,
  20. x_tolerance=x_tolerance,
  21. min_row_height=min_row_height
  22. )
  23. if not structure:
  24. st.warning("⚠️ 未检测到表格结构")
  25. st.stop()
  26. structure['modified_h_lines'] = set()
  27. structure['modified_v_lines'] = set()
  28. st.session_state.structure = structure
  29. st.session_state.undo_stack = []
  30. st.session_state.redo_stack = []
  31. clear_table_image_cache()
  32. st.success(
  33. f"✅ 检测到 {len(structure['rows'])} 行"
  34. f"({len(structure['horizontal_lines'])} 条横线),"
  35. f"{len(structure['columns'])} 列"
  36. f"({len(structure['vertical_lines'])} 条竖线)"
  37. )
  38. col1, col2, col3, col4 = st.columns(4)
  39. with col1:
  40. st.metric("行数", len(structure['rows']))
  41. with col2:
  42. st.metric("横线数", len(structure['horizontal_lines']))
  43. with col3:
  44. st.metric("列数", len(structure['columns']))
  45. with col4:
  46. st.metric("竖线数", len(structure['vertical_lines']))
  47. except Exception as e:
  48. st.error(f"❌ 分析失败: {e}")
  49. import traceback
  50. st.code(traceback.format_exc())
  51. st.stop()