Create report_generator.py
Browse files
components/report_generator.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
from fpdf import FPDF
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
class PDF(FPDF):
|
| 7 |
+
def header(self):
|
| 8 |
+
# Logo
|
| 9 |
+
# The 'logo.png' file must exist in the root directory of your project
|
| 10 |
+
try:
|
| 11 |
+
self.image('logo.png', 10, 8, 33)
|
| 12 |
+
except FileNotFoundError:
|
| 13 |
+
self.set_font('Helvetica', 'B', 12)
|
| 14 |
+
self.cell(0, 10, 'DiaSpark Logo Not Found', 0, 1, 'L')
|
| 15 |
+
# App Title
|
| 16 |
+
self.set_font('Helvetica', 'B', 20)
|
| 17 |
+
self.cell(0, 10, 'DiaSpark Health Report', 0, 1, 'C')
|
| 18 |
+
# Line break
|
| 19 |
+
self.ln(20)
|
| 20 |
+
|
| 21 |
+
def footer(self):
|
| 22 |
+
# Position at 1.5 cm from bottom
|
| 23 |
+
self.set_y(-15)
|
| 24 |
+
# Helvetica italic 8
|
| 25 |
+
self.set_font('Helvetica', 'I', 8)
|
| 26 |
+
# Page number
|
| 27 |
+
self.cell(0, 10, f'Page {self.page_no()}', 0, 0, 'C')
|
| 28 |
+
|
| 29 |
+
def create_report(user_data, input_data, prediction_result):
|
| 30 |
+
"""
|
| 31 |
+
Generates a PDF report from user data and returns the file path.
|
| 32 |
+
"""
|
| 33 |
+
pdf = PDF()
|
| 34 |
+
pdf.add_page()
|
| 35 |
+
pdf.set_font('Helvetica', '', 12)
|
| 36 |
+
|
| 37 |
+
# --- Report Header ---
|
| 38 |
+
pdf.set_font('Helvetica', 'B', 16)
|
| 39 |
+
pdf.cell(0, 10, 'Confidential Health Prediction Summary', 0, 1, 'L')
|
| 40 |
+
pdf.set_font('Helvetica', '', 12)
|
| 41 |
+
pdf.cell(0, 8, f"Report for: {user_data.get('username', 'N/A')}", 0, 1, 'L')
|
| 42 |
+
pdf.cell(0, 8, f"Date Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", 0, 1, 'L')
|
| 43 |
+
pdf.ln(10)
|
| 44 |
+
|
| 45 |
+
# --- Input Data Table ---
|
| 46 |
+
pdf.set_font('Helvetica', 'B', 14)
|
| 47 |
+
pdf.cell(0, 10, 'Submitted Health Metrics', 0, 1, 'L')
|
| 48 |
+
|
| 49 |
+
# Table Header
|
| 50 |
+
pdf.set_font('Helvetica', 'B', 11)
|
| 51 |
+
pdf.set_fill_color(230, 230, 230) # Light grey background
|
| 52 |
+
pdf.cell(95, 10, 'Metric', 1, 0, 'C', fill=True)
|
| 53 |
+
pdf.cell(95, 10, 'Value', 1, 1, 'C', fill=True)
|
| 54 |
+
|
| 55 |
+
# Table Rows
|
| 56 |
+
pdf.set_font('Helvetica', '', 11)
|
| 57 |
+
for key, value in input_data.items():
|
| 58 |
+
pdf.cell(95, 10, key, 1, 0, 'L')
|
| 59 |
+
pdf.cell(95, 10, str(value), 1, 1, 'L')
|
| 60 |
+
pdf.ln(10)
|
| 61 |
+
|
| 62 |
+
# --- Prediction Result ---
|
| 63 |
+
pdf.set_font('Helvetica', 'B', 14)
|
| 64 |
+
pdf.cell(0, 10, 'Prediction Result', 0, 1, 'L')
|
| 65 |
+
|
| 66 |
+
is_diabetic = "Diabetic" in prediction_result
|
| 67 |
+
result_text = prediction_result.replace("✅ ", "").replace("🟢 ", "") # Clean up emojis
|
| 68 |
+
|
| 69 |
+
# Set color based on result
|
| 70 |
+
if is_diabetic:
|
| 71 |
+
pdf.set_text_color(194, 8, 8) # Red
|
| 72 |
+
else:
|
| 73 |
+
pdf.set_text_color(34, 139, 34) # Green
|
| 74 |
+
|
| 75 |
+
pdf.set_font('Helvetica', 'B', 18)
|
| 76 |
+
pdf.cell(0, 15, result_text, 1, 1, 'C')
|
| 77 |
+
pdf.set_text_color(0, 0, 0) # Reset to black
|
| 78 |
+
pdf.ln(10)
|
| 79 |
+
|
| 80 |
+
# --- Disclaimer ---
|
| 81 |
+
pdf.set_font('Helvetica', 'B', 12)
|
| 82 |
+
pdf.cell(0, 10, 'IMPORTANT DISCLAIMER', 0, 1, 'L')
|
| 83 |
+
pdf.set_font('Helvetica', 'I', 10)
|
| 84 |
+
pdf.multi_cell(0, 5,
|
| 85 |
+
"This report is generated by an AI model and is for informational purposes ONLY. "
|
| 86 |
+
"It is NOT a medical diagnosis. Please consult with a qualified healthcare professional "
|
| 87 |
+
"for any health concerns or before making any decisions related to your health. "
|
| 88 |
+
"DiaSpark is not responsible for any actions taken based on this report."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# --- Save the PDF ---
|
| 92 |
+
file_path = "diaspark_report.pdf"
|
| 93 |
+
pdf.output(file_path)
|
| 94 |
+
return file_path
|