Update report.py
Browse files
report.py
CHANGED
@@ -58,28 +58,15 @@ from reportlab.lib.pagesizes import letter
|
|
58 |
from reportlab.platypus import SimpleDocTemplate, Image, Paragraph, Spacer
|
59 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
60 |
from reportlab.lib.enums import TA_JUSTIFY
|
|
|
61 |
import matplotlib.pyplot as plt
|
62 |
-
import markdown
|
63 |
-
from xml.etree import ElementTree as ET
|
64 |
|
65 |
class ReportGenerator:
|
66 |
def __init__(self):
|
67 |
self.styles = getSampleStyleSheet()
|
68 |
self.styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
|
69 |
|
70 |
-
|
71 |
-
buffer = io.BytesIO()
|
72 |
-
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
73 |
-
|
74 |
-
elements = []
|
75 |
-
|
76 |
-
elements.extend(self._add_chart(intervention_fig, "Intervention Statistics"))
|
77 |
-
elements.extend(self._add_chart(student_metrics_fig, "Student Metrics"))
|
78 |
-
elements.extend(self._add_recommendations(recommendations))
|
79 |
-
|
80 |
-
doc.build(elements)
|
81 |
-
buffer.seek(0)
|
82 |
-
return buffer
|
83 |
|
84 |
def _add_chart(self, fig, title):
|
85 |
elements = []
|
@@ -96,29 +83,26 @@ class ReportGenerator:
|
|
96 |
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
97 |
|
98 |
img_buffer.seek(0)
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
|
107 |
-
|
108 |
-
|
|
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
if elem.tag == 'h3':
|
114 |
-
elements.append(Paragraph(elem.text, self.styles['Heading3']))
|
115 |
-
elif elem.tag == 'h4':
|
116 |
-
elements.append(Paragraph(elem.text, self.styles['Heading4']))
|
117 |
-
elif elem.tag == 'p':
|
118 |
-
elements.append(Paragraph(ET.tostring(elem, encoding='unicode', method='text'), self.styles['Justify']))
|
119 |
-
elif elem.tag == 'ul':
|
120 |
-
for li in elem.findall('li'):
|
121 |
-
bullet_text = '• ' + ET.tostring(li, encoding='unicode', method='text').strip()
|
122 |
-
elements.append(Paragraph(bullet_text, self.styles['BodyText']))
|
123 |
|
|
|
|
|
|
|
|
|
|
|
124 |
return elements
|
|
|
58 |
from reportlab.platypus import SimpleDocTemplate, Image, Paragraph, Spacer
|
59 |
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
60 |
from reportlab.lib.enums import TA_JUSTIFY
|
61 |
+
from reportlab.lib.units import inch
|
62 |
import matplotlib.pyplot as plt
|
|
|
|
|
63 |
|
64 |
class ReportGenerator:
|
65 |
def __init__(self):
|
66 |
self.styles = getSampleStyleSheet()
|
67 |
self.styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
|
68 |
|
69 |
+
# ... (other methods remain the same)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
def _add_chart(self, fig, title):
|
72 |
elements = []
|
|
|
83 |
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
84 |
|
85 |
img_buffer.seek(0)
|
86 |
+
img = Image(img_buffer)
|
87 |
+
|
88 |
+
# Calculate width and height to maintain aspect ratio
|
89 |
+
max_width = 6.5 * inch # Maximum width (letter width is 8.5 inches, leaving margins)
|
90 |
+
max_height = 4 * inch # Maximum height
|
91 |
+
|
92 |
+
img_width, img_height = img.getSize()
|
93 |
+
aspect = img_width / float(img_height)
|
94 |
|
95 |
+
if img_width > max_width:
|
96 |
+
img_width = max_width
|
97 |
+
img_height = img_width / aspect
|
98 |
|
99 |
+
if img_height > max_height:
|
100 |
+
img_height = max_height
|
101 |
+
img_width = img_height * aspect
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
+
img.drawWidth = img_width
|
104 |
+
img.drawHeight = img_height
|
105 |
+
|
106 |
+
elements.append(img)
|
107 |
+
elements.append(Spacer(1, 12))
|
108 |
return elements
|