File size: 1,615 Bytes
1ce8e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# report.py

import io
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Image, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet

class ReportGenerator:
    def __init__(self):
        self.styles = getSampleStyleSheet()

    def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
        buffer = io.BytesIO()
        doc = SimpleDocTemplate(buffer, pagesize=letter)
        
        elements = []
        
        # Add the intervention statistics chart
        elements.extend(self._add_chart(intervention_fig, "Intervention Statistics"))
        
        # Add the student metrics chart
        elements.extend(self._add_chart(student_metrics_fig, "Student Metrics"))
        
        # Add the AI recommendations
        elements.extend(self._add_recommendations(recommendations))
        
        # Build the PDF
        doc.build(elements)
        buffer.seek(0)
        return buffer

    def _add_chart(self, fig, title):
        elements = []
        elements.append(Paragraph(title, self.styles['Heading2']))
        img_buffer = io.BytesIO()
        fig.write_image(img_buffer, format="png")
        img_buffer.seek(0)
        elements.append(Image(img_buffer, width=500, height=300))
        elements.append(Spacer(1, 12))
        return elements

    def _add_recommendations(self, recommendations):
        elements = []
        elements.append(Paragraph("AI Recommendations", self.styles['Heading1']))
        elements.append(Paragraph(recommendations, self.styles['BodyText']))
        return elements