DrishtiSharma commited on
Commit
d22e0a5
Β·
verified Β·
1 Parent(s): 2c443f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -33
app.py CHANGED
@@ -110,44 +110,36 @@ def create_pdf_report_with_viz(report, conclusion, visualizations):
110
  pdf.set_font("Arial", size=12)
111
  pdf.multi_cell(0, 10, conclusion)
112
 
113
- # Add Visualizations with Descriptions
114
  pdf.add_page()
115
  pdf.set_font("Arial", style="B", size=16)
116
  pdf.cell(0, 10, "πŸ“ˆ Visualizations", ln=True)
117
  pdf.ln(5)
118
 
119
- temp_images = [] # Store temp image paths for cleanup
120
-
121
- for i, fig in enumerate(visualizations, start=1):
122
- # Extract Figure Title and Axis Labels
123
- fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
124
- x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
125
- y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
126
-
127
- # Save the figure temporarily
128
- img_temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
129
- fig.write_image(img_temp.name)
130
- temp_images.append(img_temp.name) # Keep track for cleanup
131
-
132
- # Insert Title and Description
133
- pdf.set_font("Arial", style="B", size=14)
134
- pdf.multi_cell(0, 10, f"{i}. {fig_title}")
135
- pdf.set_font("Arial", size=12)
136
- pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
137
- pdf.ln(3)
138
-
139
- # Embed Visualization with Scaling
140
- pdf.image(img_temp.name, w=170) # Auto-scaled width to fit the page
141
- pdf.ln(10)
142
-
143
- # Save the PDF to a temporary file
144
- temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
145
- pdf.output(temp_pdf.name)
146
-
147
- # Clean up temporary images
148
- for img_path in temp_images:
149
- if os.path.exists(img_path):
150
- os.remove(img_path)
151
 
152
  return temp_pdf
153
 
 
110
  pdf.set_font("Arial", size=12)
111
  pdf.multi_cell(0, 10, conclusion)
112
 
113
+ # Add Visualizations
114
  pdf.add_page()
115
  pdf.set_font("Arial", style="B", size=16)
116
  pdf.cell(0, 10, "πŸ“ˆ Visualizations", ln=True)
117
  pdf.ln(5)
118
 
119
+ with tempfile.TemporaryDirectory() as temp_dir:
120
+ for i, fig in enumerate(visualizations, start=1):
121
+ fig_title = fig.layout.title.text if fig.layout.title.text else f"Visualization {i}"
122
+ x_axis = fig.layout.xaxis.title.text if fig.layout.xaxis.title.text else "X-axis"
123
+ y_axis = fig.layout.yaxis.title.text if fig.layout.yaxis.title.text else "Y-axis"
124
+
125
+ # Save each visualization as a PNG image
126
+ img_path = os.path.join(temp_dir, f"viz_{i}.png")
127
+ fig.write_image(img_path)
128
+
129
+ # Insert Title and Description
130
+ pdf.set_font("Arial", style="B", size=14)
131
+ pdf.multi_cell(0, 10, f"{i}. {fig_title}")
132
+ pdf.set_font("Arial", size=12)
133
+ pdf.multi_cell(0, 10, f"X-axis: {x_axis} | Y-axis: {y_axis}")
134
+ pdf.ln(3)
135
+
136
+ # Embed Visualization
137
+ pdf.image(img_path, w=170)
138
+ pdf.ln(10)
139
+
140
+ # Save PDF
141
+ temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
142
+ pdf.output(temp_pdf.name)
 
 
 
 
 
 
 
 
143
 
144
  return temp_pdf
145