Update report.py
Browse files
report.py
CHANGED
|
@@ -60,13 +60,27 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
def _add_chart(self, fig, title):
|
| 72 |
elements = []
|
|
@@ -105,4 +119,27 @@ class ReportGenerator:
|
|
| 105 |
|
| 106 |
elements.append(img)
|
| 107 |
elements.append(Spacer(1, 12))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
return elements
|
|
|
|
| 60 |
from reportlab.lib.enums import TA_JUSTIFY
|
| 61 |
from reportlab.lib.units import inch
|
| 62 |
import matplotlib.pyplot as plt
|
| 63 |
+
import markdown
|
| 64 |
+
from xml.etree import ElementTree as ET
|
| 65 |
|
| 66 |
class ReportGenerator:
|
| 67 |
def __init__(self):
|
| 68 |
self.styles = getSampleStyleSheet()
|
| 69 |
self.styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
|
| 70 |
|
| 71 |
+
def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
|
| 72 |
+
buffer = io.BytesIO()
|
| 73 |
+
doc = SimpleDocTemplate(buffer, pagesize=letter)
|
| 74 |
+
|
| 75 |
+
elements = []
|
| 76 |
+
|
| 77 |
+
elements.extend(self._add_chart(intervention_fig, "Intervention Statistics"))
|
| 78 |
+
elements.extend(self._add_chart(student_metrics_fig, "Student Metrics"))
|
| 79 |
+
elements.extend(self._add_recommendations(recommendations))
|
| 80 |
+
|
| 81 |
+
doc.build(elements)
|
| 82 |
+
buffer.seek(0)
|
| 83 |
+
return buffer
|
| 84 |
|
| 85 |
def _add_chart(self, fig, title):
|
| 86 |
elements = []
|
|
|
|
| 119 |
|
| 120 |
elements.append(img)
|
| 121 |
elements.append(Spacer(1, 12))
|
| 122 |
+
return elements
|
| 123 |
+
|
| 124 |
+
def _add_recommendations(self, recommendations):
|
| 125 |
+
elements = []
|
| 126 |
+
elements.append(Paragraph("AI Recommendations", self.styles['Heading1']))
|
| 127 |
+
|
| 128 |
+
# Convert markdown to HTML
|
| 129 |
+
html = markdown.markdown(recommendations)
|
| 130 |
+
|
| 131 |
+
# Parse HTML and convert to ReportLab paragraphs
|
| 132 |
+
root = ET.fromstring(html)
|
| 133 |
+
for elem in root.iter():
|
| 134 |
+
if elem.tag == 'h3':
|
| 135 |
+
elements.append(Paragraph(elem.text, self.styles['Heading3']))
|
| 136 |
+
elif elem.tag == 'h4':
|
| 137 |
+
elements.append(Paragraph(elem.text, self.styles['Heading4']))
|
| 138 |
+
elif elem.tag == 'p':
|
| 139 |
+
elements.append(Paragraph(ET.tostring(elem, encoding='unicode', method='text'), self.styles['Justify']))
|
| 140 |
+
elif elem.tag == 'ul':
|
| 141 |
+
for li in elem.findall('li'):
|
| 142 |
+
bullet_text = '• ' + ET.tostring(li, encoding='unicode', method='text').strip()
|
| 143 |
+
elements.append(Paragraph(bullet_text, self.styles['BodyText']))
|
| 144 |
+
|
| 145 |
return elements
|