Update report.py
Browse files
report.py
CHANGED
@@ -1,12 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import io
|
2 |
from reportlab.lib.pagesizes import letter
|
3 |
from reportlab.platypus import SimpleDocTemplate, Image, Paragraph, Spacer
|
4 |
-
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
5 |
import matplotlib.pyplot as plt
|
|
|
|
|
6 |
|
7 |
class ReportGenerator:
|
8 |
def __init__(self):
|
9 |
self.styles = getSampleStyleSheet()
|
|
|
10 |
|
11 |
def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
|
12 |
buffer = io.BytesIO()
|
@@ -14,16 +73,10 @@ class ReportGenerator:
|
|
14 |
|
15 |
elements = []
|
16 |
|
17 |
-
# Add the intervention statistics chart
|
18 |
elements.extend(self._add_chart(intervention_fig, "Intervention Statistics"))
|
19 |
-
|
20 |
-
# Add the student metrics chart
|
21 |
elements.extend(self._add_chart(student_metrics_fig, "Student Metrics"))
|
22 |
-
|
23 |
-
# Add the AI recommendations
|
24 |
elements.extend(self._add_recommendations(recommendations))
|
25 |
|
26 |
-
# Build the PDF
|
27 |
doc.build(elements)
|
28 |
buffer.seek(0)
|
29 |
return buffer
|
@@ -34,20 +87,38 @@ class ReportGenerator:
|
|
34 |
img_buffer = io.BytesIO()
|
35 |
|
36 |
if hasattr(fig, 'write_image'): # Plotly figure
|
37 |
-
fig.write_image(img_buffer, format="png")
|
38 |
elif isinstance(fig, plt.Figure): # Matplotlib figure
|
39 |
-
fig.
|
40 |
-
|
|
|
41 |
else:
|
42 |
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
43 |
|
44 |
img_buffer.seek(0)
|
45 |
-
elements.append(Image(img_buffer, width=500, height=300))
|
46 |
elements.append(Spacer(1, 12))
|
47 |
return elements
|
48 |
|
49 |
def _add_recommendations(self, recommendations):
|
50 |
elements = []
|
51 |
elements.append(Paragraph("AI Recommendations", self.styles['Heading1']))
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
return elements
|
|
|
1 |
+
# import io
|
2 |
+
# from reportlab.lib.pagesizes import letter
|
3 |
+
# from reportlab.platypus import SimpleDocTemplate, Image, Paragraph, Spacer
|
4 |
+
# from reportlab.lib.styles import getSampleStyleSheet
|
5 |
+
# import matplotlib.pyplot as plt
|
6 |
+
|
7 |
+
# class ReportGenerator:
|
8 |
+
# def __init__(self):
|
9 |
+
# self.styles = getSampleStyleSheet()
|
10 |
+
|
11 |
+
# def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
|
12 |
+
# buffer = io.BytesIO()
|
13 |
+
# doc = SimpleDocTemplate(buffer, pagesize=letter)
|
14 |
+
|
15 |
+
# elements = []
|
16 |
+
|
17 |
+
# # Add the intervention statistics chart
|
18 |
+
# elements.extend(self._add_chart(intervention_fig, "Intervention Statistics"))
|
19 |
+
|
20 |
+
# # Add the student metrics chart
|
21 |
+
# elements.extend(self._add_chart(student_metrics_fig, "Student Metrics"))
|
22 |
+
|
23 |
+
# # Add the AI recommendations
|
24 |
+
# elements.extend(self._add_recommendations(recommendations))
|
25 |
+
|
26 |
+
# # Build the PDF
|
27 |
+
# doc.build(elements)
|
28 |
+
# buffer.seek(0)
|
29 |
+
# return buffer
|
30 |
+
|
31 |
+
# def _add_chart(self, fig, title):
|
32 |
+
# elements = []
|
33 |
+
# elements.append(Paragraph(title, self.styles['Heading2']))
|
34 |
+
# img_buffer = io.BytesIO()
|
35 |
+
|
36 |
+
# if hasattr(fig, 'write_image'): # Plotly figure
|
37 |
+
# fig.write_image(img_buffer, format="png")
|
38 |
+
# elif isinstance(fig, plt.Figure): # Matplotlib figure
|
39 |
+
# fig.savefig(img_buffer, format='png')
|
40 |
+
# plt.close(fig) # Close the figure to free up memory
|
41 |
+
# else:
|
42 |
+
# raise ValueError(f"Unsupported figure type: {type(fig)}")
|
43 |
+
|
44 |
+
# img_buffer.seek(0)
|
45 |
+
# elements.append(Image(img_buffer, width=500, height=300))
|
46 |
+
# elements.append(Spacer(1, 12))
|
47 |
+
# return elements
|
48 |
+
|
49 |
+
# def _add_recommendations(self, recommendations):
|
50 |
+
# elements = []
|
51 |
+
# elements.append(Paragraph("AI Recommendations", self.styles['Heading1']))
|
52 |
+
# elements.append(Paragraph(recommendations, self.styles['BodyText']))
|
53 |
+
# return elements
|
54 |
+
|
55 |
+
|
56 |
import io
|
57 |
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 |
def create_combined_pdf(self, intervention_fig, student_metrics_fig, recommendations):
|
71 |
buffer = io.BytesIO()
|
|
|
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
|
|
|
87 |
img_buffer = io.BytesIO()
|
88 |
|
89 |
if hasattr(fig, 'write_image'): # Plotly figure
|
90 |
+
fig.write_image(img_buffer, format="png", width=700, height=400)
|
91 |
elif isinstance(fig, plt.Figure): # Matplotlib figure
|
92 |
+
fig.set_size_inches(10, 6) # Set a consistent size
|
93 |
+
fig.savefig(img_buffer, format='png', dpi=100, bbox_inches='tight')
|
94 |
+
plt.close(fig)
|
95 |
else:
|
96 |
raise ValueError(f"Unsupported figure type: {type(fig)}")
|
97 |
|
98 |
img_buffer.seek(0)
|
99 |
+
elements.append(Image(img_buffer, width=500, height=300, keepAspectRatio=True))
|
100 |
elements.append(Spacer(1, 12))
|
101 |
return elements
|
102 |
|
103 |
def _add_recommendations(self, recommendations):
|
104 |
elements = []
|
105 |
elements.append(Paragraph("AI Recommendations", self.styles['Heading1']))
|
106 |
+
|
107 |
+
# Convert markdown to HTML
|
108 |
+
html = markdown.markdown(recommendations)
|
109 |
+
|
110 |
+
# Parse HTML and convert to ReportLab paragraphs
|
111 |
+
root = ET.fromstring(html)
|
112 |
+
for elem in root.iter():
|
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
|