Update report.py
Browse files
report.py
CHANGED
@@ -122,23 +122,25 @@ import matplotlib.pyplot as plt
|
|
122 |
import markdown
|
123 |
from bs4 import BeautifulSoup
|
124 |
from PIL import Image as PILImage
|
|
|
125 |
|
126 |
class ReportGenerator:
|
127 |
def __init__(self):
|
128 |
self.styles = getSampleStyleSheet()
|
129 |
self.styles['BodyText'].alignment = TA_JUSTIFY
|
130 |
|
131 |
-
# Modify existing styles instead of adding new ones
|
132 |
self.styles['Bullet'].leftIndent = 20
|
133 |
self.styles['Bullet'].firstLineIndent = 0
|
134 |
self.styles['Bullet'].alignment = TA_LEFT
|
135 |
|
136 |
-
# Adjust heading styles for better hierarchy
|
137 |
self.styles['Heading1'].fontSize = 18
|
138 |
self.styles['Heading2'].fontSize = 16
|
139 |
self.styles['Heading3'].fontSize = 14
|
140 |
self.styles['Heading4'].fontSize = 12
|
141 |
|
|
|
|
|
|
|
142 |
def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
|
143 |
buffer = io.BytesIO()
|
144 |
doc = SimpleDocTemplate(buffer, pagesize=letter, topMargin=0.5*inch, bottomMargin=0.5*inch)
|
@@ -154,57 +156,13 @@ class ReportGenerator:
|
|
154 |
return buffer
|
155 |
|
156 |
def _add_chart(self, fig, title):
|
157 |
-
|
158 |
-
elements.append(Paragraph(title, self.styles['Heading2']))
|
159 |
-
img_buffer = io.BytesIO()
|
160 |
-
|
161 |
-
if hasattr(fig, 'write_image'): # Plotly figure
|
162 |
-
fig.write_image(img_buffer, format="png", width=700, height=400)
|
163 |
-
elif isinstance(fig, plt.Figure): # Matplotlib figure
|
164 |
-
fig.set_size_inches(10, 6) # Set a consistent size
|
165 |
-
fig.savefig(img_buffer, format='png', dpi=100, bbox_inches='tight')
|
166 |
-
plt.close(fig)
|
167 |
-
else:
|
168 |
-
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
169 |
-
|
170 |
-
img_buffer.seek(0)
|
171 |
-
|
172 |
-
# Use PIL to get image dimensions
|
173 |
-
with PILImage.open(img_buffer) as img:
|
174 |
-
img_width, img_height = img.size
|
175 |
-
|
176 |
-
# Calculate width and height to maintain aspect ratio
|
177 |
-
max_width = 6.5 * inch # Maximum width (letter width is 8.5 inches, leaving margins)
|
178 |
-
max_height = 4 * inch # Maximum height
|
179 |
-
|
180 |
-
aspect = img_width / float(img_height)
|
181 |
-
|
182 |
-
if img_width > max_width:
|
183 |
-
img_width = max_width
|
184 |
-
img_height = img_width / aspect
|
185 |
-
|
186 |
-
if img_height > max_height:
|
187 |
-
img_height = max_height
|
188 |
-
img_width = img_height * aspect
|
189 |
-
|
190 |
-
# Reset buffer position
|
191 |
-
img_buffer.seek(0)
|
192 |
-
|
193 |
-
# Create ReportLab Image with calculated dimensions
|
194 |
-
img = Image(img_buffer, width=img_width, height=img_height)
|
195 |
-
|
196 |
-
elements.append(img)
|
197 |
-
elements.append(Spacer(1, 12))
|
198 |
-
return elements
|
199 |
|
200 |
def _add_recommendations(self, recommendations):
|
201 |
elements = []
|
202 |
elements.append(Paragraph("MTSS.ai Analysis", self.styles['Heading1']))
|
203 |
|
204 |
-
# Convert markdown to HTML
|
205 |
html = markdown.markdown(recommendations)
|
206 |
-
|
207 |
-
# Parse HTML
|
208 |
soup = BeautifulSoup(html, 'html.parser')
|
209 |
|
210 |
for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'ul']):
|
@@ -213,11 +171,24 @@ class ReportGenerator:
|
|
213 |
style = f'Heading{min(level, 4)}'
|
214 |
elements.append(Paragraph(element.text, self.styles[style]))
|
215 |
elif element.name == 'p':
|
216 |
-
elements.append(
|
217 |
elif element.name == 'ul':
|
218 |
for li in element.find_all('li'):
|
219 |
-
bullet_text = '• ' + li
|
220 |
elements.append(Paragraph(bullet_text, self.styles['Bullet']))
|
221 |
elements.append(Spacer(1, 6))
|
222 |
|
223 |
-
return elements
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
import markdown
|
123 |
from bs4 import BeautifulSoup
|
124 |
from PIL import Image as PILImage
|
125 |
+
from reportlab.lib.colors import black
|
126 |
|
127 |
class ReportGenerator:
|
128 |
def __init__(self):
|
129 |
self.styles = getSampleStyleSheet()
|
130 |
self.styles['BodyText'].alignment = TA_JUSTIFY
|
131 |
|
|
|
132 |
self.styles['Bullet'].leftIndent = 20
|
133 |
self.styles['Bullet'].firstLineIndent = 0
|
134 |
self.styles['Bullet'].alignment = TA_LEFT
|
135 |
|
|
|
136 |
self.styles['Heading1'].fontSize = 18
|
137 |
self.styles['Heading2'].fontSize = 16
|
138 |
self.styles['Heading3'].fontSize = 14
|
139 |
self.styles['Heading4'].fontSize = 12
|
140 |
|
141 |
+
# Add a new style for bold text
|
142 |
+
self.styles.add(ParagraphStyle(name='Bold', parent=self.styles['BodyText'], fontName='Helvetica-Bold'))
|
143 |
+
|
144 |
def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
|
145 |
buffer = io.BytesIO()
|
146 |
doc = SimpleDocTemplate(buffer, pagesize=letter, topMargin=0.5*inch, bottomMargin=0.5*inch)
|
|
|
156 |
return buffer
|
157 |
|
158 |
def _add_chart(self, fig, title):
|
159 |
+
# ... (unchanged)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
def _add_recommendations(self, recommendations):
|
162 |
elements = []
|
163 |
elements.append(Paragraph("MTSS.ai Analysis", self.styles['Heading1']))
|
164 |
|
|
|
165 |
html = markdown.markdown(recommendations)
|
|
|
|
|
166 |
soup = BeautifulSoup(html, 'html.parser')
|
167 |
|
168 |
for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'ul']):
|
|
|
171 |
style = f'Heading{min(level, 4)}'
|
172 |
elements.append(Paragraph(element.text, self.styles[style]))
|
173 |
elif element.name == 'p':
|
174 |
+
elements.append(self._create_paragraph_with_inline_styles(element))
|
175 |
elif element.name == 'ul':
|
176 |
for li in element.find_all('li'):
|
177 |
+
bullet_text = '• ' + self._get_text_with_inline_styles(li)
|
178 |
elements.append(Paragraph(bullet_text, self.styles['Bullet']))
|
179 |
elements.append(Spacer(1, 6))
|
180 |
|
181 |
+
return elements
|
182 |
+
|
183 |
+
def _create_paragraph_with_inline_styles(self, element):
|
184 |
+
text = self._get_text_with_inline_styles(element)
|
185 |
+
return Paragraph(text, self.styles['BodyText'])
|
186 |
+
|
187 |
+
def _get_text_with_inline_styles(self, element):
|
188 |
+
text = ""
|
189 |
+
for child in element.children:
|
190 |
+
if isinstance(child, str):
|
191 |
+
text += child
|
192 |
+
elif child.name == 'strong' or child.name == 'b':
|
193 |
+
text += f'<b>{child.text}</b>'
|
194 |
+
return text
|