Update report.py
Browse files
report.py
CHANGED
@@ -156,7 +156,48 @@ class ReportGenerator:
|
|
156 |
return buffer
|
157 |
|
158 |
def _add_chart(self, fig, title):
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
def _add_recommendations(self, recommendations):
|
162 |
elements = []
|
@@ -189,6 +230,10 @@ class ReportGenerator:
|
|
189 |
for child in element.children:
|
190 |
if isinstance(child, str):
|
191 |
text += child
|
192 |
-
elif child.name
|
193 |
text += f'<b>{child.text}</b>'
|
|
|
|
|
|
|
|
|
194 |
return text
|
|
|
156 |
return buffer
|
157 |
|
158 |
def _add_chart(self, fig, title):
|
159 |
+
elements = []
|
160 |
+
elements.append(Paragraph(title, self.styles['Heading2']))
|
161 |
+
img_buffer = io.BytesIO()
|
162 |
+
|
163 |
+
if hasattr(fig, 'write_image'): # Plotly figure
|
164 |
+
fig.write_image(img_buffer, format="png", width=700, height=400)
|
165 |
+
elif isinstance(fig, plt.Figure): # Matplotlib figure
|
166 |
+
fig.set_size_inches(10, 6) # Set a consistent size
|
167 |
+
fig.savefig(img_buffer, format='png', dpi=100, bbox_inches='tight')
|
168 |
+
plt.close(fig)
|
169 |
+
else:
|
170 |
+
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
171 |
+
|
172 |
+
img_buffer.seek(0)
|
173 |
+
|
174 |
+
# Use PIL to get image dimensions
|
175 |
+
with PILImage.open(img_buffer) as img:
|
176 |
+
img_width, img_height = img.size
|
177 |
+
|
178 |
+
# Calculate width and height to maintain aspect ratio
|
179 |
+
max_width = 6.5 * inch # Maximum width (letter width is 8.5 inches, leaving margins)
|
180 |
+
max_height = 4 * inch # Maximum height
|
181 |
+
|
182 |
+
aspect = img_width / float(img_height)
|
183 |
+
|
184 |
+
if img_width > max_width:
|
185 |
+
img_width = max_width
|
186 |
+
img_height = img_width / aspect
|
187 |
+
|
188 |
+
if img_height > max_height:
|
189 |
+
img_height = max_height
|
190 |
+
img_width = img_height * aspect
|
191 |
+
|
192 |
+
# Reset buffer position
|
193 |
+
img_buffer.seek(0)
|
194 |
+
|
195 |
+
# Create ReportLab Image with calculated dimensions
|
196 |
+
img = Image(img_buffer, width=img_width, height=img_height)
|
197 |
+
|
198 |
+
elements.append(img)
|
199 |
+
elements.append(Spacer(1, 12))
|
200 |
+
return elements
|
201 |
|
202 |
def _add_recommendations(self, recommendations):
|
203 |
elements = []
|
|
|
230 |
for child in element.children:
|
231 |
if isinstance(child, str):
|
232 |
text += child
|
233 |
+
elif child.name in ['strong', 'b']:
|
234 |
text += f'<b>{child.text}</b>'
|
235 |
+
elif child.name in ['em', 'i']:
|
236 |
+
text += f'<i>{child.text}</i>'
|
237 |
+
elif child.name == 'u':
|
238 |
+
text += f'<u>{child.text}</u>'
|
239 |
return text
|