feat: Enhance ExportManager with landscape PDF support and improved graph handling

This commit is contained in:
William Valentin
2025-08-06 12:36:56 -07:00
parent af747c4008
commit bb70aff24f
2 changed files with 611 additions and 31 deletions

View File

@@ -18,11 +18,12 @@ from xml.etree.ElementTree import Element, SubElement, tostring
import pandas as pd
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.pagesizes import A4, landscape
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.units import inch
from reportlab.platypus import (
Image,
PageBreak,
Paragraph,
SimpleDocTemplate,
Spacer,
@@ -178,13 +179,23 @@ class ExportManager:
edgecolor="none",
)
# Verify the file was actually created
# Ensure the figure data is properly flushed to disk
import matplotlib.pyplot as plt
plt.draw()
plt.pause(0.01) # Small pause to ensure file is written
# Verify the file was actually created and has content
if not temp_image_path.exists():
self.logger.error(
f"Graph image file was not created: {temp_image_path}"
)
return None
if temp_image_path.stat().st_size == 0:
self.logger.error(f"Graph image file is empty: {temp_image_path}")
return None
self.logger.info(f"Graph image saved successfully: {temp_image_path}")
return str(temp_image_path)
@@ -197,10 +208,10 @@ class ExportManager:
try:
df = self.data_manager.load_data()
# Create PDF document
# Create PDF document in landscape format for better table/graph display
doc = SimpleDocTemplate(
export_path,
pagesize=A4,
pagesize=landscape(A4),
rightMargin=72,
leftMargin=72,
topMargin=72,
@@ -252,24 +263,26 @@ class ExportManager:
# Include graph if requested and available
if include_graph:
temp_dir = Path(export_path).parent / "temp_export"
graph_path = None
try:
graph_path = self._save_graph_as_image(temp_dir)
if graph_path and os.path.exists(graph_path):
# Add page break before graph for full page display
story.append(PageBreak())
story.append(
Paragraph("Data Visualization", styles["Heading2"])
)
story.append(Spacer(1, 10))
# Add graph image
img = Image(graph_path, width=6 * inch, height=3.6 * inch)
story.append(img)
story.append(Spacer(1, 20))
# Clean up temp image
os.remove(graph_path)
# Full page graph - maintain proportions while maximizing size
# Let ReportLab scale proportionally to fit landscape page
img = Image(graph_path, width=9 * inch, height=5.4 * inch)
story.append(img)
else:
# Graph not available, add a note instead
story.append(PageBreak())
story.append(
Paragraph("Data Visualization", styles["Heading2"])
)
@@ -281,11 +294,11 @@ class ExportManager:
styles["Normal"],
)
)
story.append(Spacer(1, 20))
except Exception as e:
self.logger.error(f"Error including graph in PDF: {str(e)}")
# Add error note instead of failing completely
story.append(PageBreak())
story.append(Paragraph("Data Visualization", styles["Heading2"]))
story.append(Spacer(1, 10))
story.append(
@@ -293,20 +306,15 @@ class ExportManager:
f"Graph could not be included: {str(e)}", styles["Normal"]
)
)
story.append(Spacer(1, 20))
finally:
# Clean up temp directory
if temp_dir.exists():
with contextlib.suppress(OSError):
temp_dir.rmdir()
# Add data table if we have data
if not df.empty:
# Start table on new page
story.append(PageBreak())
story.append(Paragraph("Data Table", styles["Heading2"]))
story.append(Spacer(1, 10))
story.append(Spacer(1, 20))
# Prepare table data - limit columns for better PDF formatting
# Prepare table data - include all columns for full display
display_columns = ["date"]
for pathology_key in self.pathology_manager.get_pathology_keys():
display_columns.append(pathology_key)
@@ -320,11 +328,8 @@ class ExportManager:
]
display_df = df[available_columns].copy()
# Truncate long notes for better table formatting
if "note" in display_df.columns:
display_df["note"] = display_df["note"].apply(
lambda x: (str(x)[:50] + "...") if len(str(x)) > 50 else str(x)
)
# Don't truncate notes - landscape format has full width
# Keep notes as-is for complete data visibility
# Convert to table data
table_data = [available_columns] # Headers
@@ -333,28 +338,57 @@ class ExportManager:
[str(val) if pd.notna(val) else "" for val in row]
)
# Create table with styling
table = Table(table_data, repeatRows=1)
# Calculate optimal column widths for landscape format
col_widths = []
for col in available_columns:
if col == "date":
col_widths.append(1.0 * inch) # Fixed width for dates
elif col == "note":
col_widths.append(3.5 * inch) # Wider for notes
elif col in self.pathology_manager.get_pathology_keys():
col_widths.append(0.8 * inch) # Narrow for pathology scores
elif col in self.medicine_manager.get_medicine_keys():
col_widths.append(0.8 * inch) # Narrow for medicine status
else:
col_widths.append(1.0 * inch) # Default width
# Create table with specified column widths and better styling
table = Table(table_data, colWidths=col_widths, repeatRows=1)
table.setStyle(
TableStyle(
[
("BACKGROUND", (0, 0), (-1, 0), colors.grey),
("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),
("ALIGN", (0, 0), (-1, -1), "CENTER"),
# Left align for better readability
("ALIGN", (0, 0), (-1, -1), "LEFT"),
("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"),
("FONTSIZE", (0, 0), (-1, 0), 10),
("BOTTOMPADDING", (0, 0), (-1, 0), 12),
# Add more padding for better readability
("LEFTPADDING", (0, 0), (-1, -1), 8),
("RIGHTPADDING", (0, 0), (-1, -1), 8),
("TOPPADDING", (0, 0), (-1, -1), 6),
("BOTTOMPADDING", (0, 0), (-1, -1), 6),
("BACKGROUND", (0, 1), (-1, -1), colors.beige),
("FONTNAME", (0, 1), (-1, -1), "Helvetica"),
("FONTSIZE", (0, 1), (-1, -1), 8),
# Slightly larger font for better readability
("FONTSIZE", (0, 1), (-1, -1), 9),
("GRID", (0, 0), (-1, -1), 1, colors.black),
("VALIGN", (0, 0), (-1, -1), "TOP"),
("WORDWRAP", (0, 0), (-1, -1), True),
# Alternating row colors for better visual separation
(
"ROWBACKGROUNDS",
(0, 1),
(-1, -1),
[colors.beige, colors.lightgrey],
),
]
)
)
story.append(table)
else:
story.append(PageBreak())
story.append(
Paragraph("No data available to export.", styles["Normal"])
)
@@ -362,6 +396,21 @@ class ExportManager:
# Build PDF
doc.build(story)
# Clean up temporary image file after PDF is built
if include_graph:
temp_dir = Path(export_path).parent / "temp_export"
if graph_path and os.path.exists(graph_path):
try:
os.remove(graph_path)
self.logger.debug(f"Cleaned up temporary image: {graph_path}")
except OSError as e:
self.logger.warning(f"Could not remove temp image: {e}")
# Clean up temp directory if empty
if temp_dir.exists():
with contextlib.suppress(OSError):
temp_dir.rmdir()
self.logger.info(f"Data exported to PDF: {export_path}")
return True