table_viewer.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. 表格结构渲染视图
  3. """
  4. import streamlit as st
  5. from typing import Dict
  6. from .drawing import get_cached_table_lines_image
  7. from .viewer import show_image_with_scroll
  8. from .adjustments import create_adjustment_section
  9. def render_table_structure_view(
  10. structure: Dict,
  11. image,
  12. line_width: int,
  13. display_mode: str,
  14. zoom_level: float,
  15. show_line_numbers: bool,
  16. viewport_width: int,
  17. viewport_height: int
  18. ):
  19. """
  20. 渲染表格结构视图(统一三种模式的显示逻辑)
  21. Args:
  22. structure: 表格结构
  23. image: 图片对象
  24. line_width: 线条宽度
  25. display_mode: 显示模式
  26. zoom_level: 缩放级别
  27. show_line_numbers: 是否显示线条编号
  28. viewport_width: 视口宽度
  29. viewport_height: 视口高度
  30. """
  31. # 绘制表格线
  32. img_with_lines = get_cached_table_lines_image(
  33. image, structure, line_width=line_width, show_numbers=show_line_numbers
  34. )
  35. # 根据显示模式显示图片
  36. if display_mode == "对比显示":
  37. col1, col2 = st.columns(2)
  38. with col1:
  39. show_image_with_scroll(
  40. image, "原图",
  41. viewport_width, viewport_height, zoom_level
  42. )
  43. with col2:
  44. show_image_with_scroll(
  45. img_with_lines, "表格线",
  46. viewport_width, viewport_height, zoom_level
  47. )
  48. elif display_mode == "仅显示划线图":
  49. show_image_with_scroll(
  50. img_with_lines,
  51. f"表格线图 (缩放: {zoom_level:.0%})",
  52. viewport_width,
  53. viewport_height,
  54. zoom_level
  55. )
  56. else: # 仅显示原图
  57. show_image_with_scroll(
  58. image,
  59. f"原图 (缩放: {zoom_level:.0%})",
  60. viewport_width,
  61. viewport_height,
  62. zoom_level
  63. )
  64. # 手动调整区域
  65. create_adjustment_section(structure)
  66. # 显示详细信息
  67. with st.expander("📊 表格结构详情"):
  68. st.json({
  69. "行数": len(structure['rows']),
  70. "列数": len(structure['columns']),
  71. "横线数": len(structure.get('horizontal_lines', [])),
  72. "竖线数": len(structure.get('vertical_lines', [])),
  73. "横线坐标": structure.get('horizontal_lines', []),
  74. "竖线坐标": structure.get('vertical_lines', []),
  75. "标准行高": structure.get('row_height'),
  76. "列宽度": structure.get('col_widths'),
  77. "修改的横线": list(structure.get('modified_h_lines', set())),
  78. "修改的竖线": list(structure.get('modified_v_lines', set()))
  79. })