from jinja2 import Environment, FileSystemLoader from weasyprint import HTML import matplotlib.pyplot as plt import os # 1. Generate a chart with matplotlib os.makedirs("static/charts", exist_ok=True) chart_path = "static/charts/resp_chart.png" plt.plot([1, 2, 3, 4], [1, 4, 2, 5], label="Breath Volume") plt.legend() plt.title("Respiratory Chart") plt.savefig(chart_path) plt.close() # 2. Patient data (this would usually come from your DB) patient_data = { "name": "Keirstyn Moran", "age": 34, "height": 163, "weight": 56 } context = { "patient": patient_data, "indications": "No Respiratory Capacity Limitation", "chart_path": chart_path } # 3. Render Jinja2 template env = Environment(loader=FileSystemLoader("templates")) template = env.get_template("report.html") html_out = template.render(context) # 4. Generate PDF HTML(string=html_out, base_url=".").write_pdf("lung_report.pdf") print("✅ Report generated: lung_report.pdf")