add PDF export (FR10)
pdf_export.py builds a PDF report (summary stats, AGP chart, hypo/hyper event summaries) via fpdf2 + matplotlib rather than plotly's kaleido, to avoid bundling a Chromium build into the image just for this. The Export page lets the user pick which sections to include and a date range, then download the result to send to the hospital team. This completes all ten functional requirements in docs/requirements-reporting.md. Co-Authored-By: Claude Sonnet 5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import requests
|
||||
import streamlit as st
|
||||
|
||||
from auth import require_auth
|
||||
from nightscout_client import NightscoutConfigError
|
||||
from pdf_export import AVAILABLE_SECTIONS, SECTION_EVENTS, build_report_pdf
|
||||
from reporting import HIGH, LOW, VERY_HIGH, VERY_LOW, detect_events, summarize_events
|
||||
from ui_common import load_range_data, select_period_days
|
||||
|
||||
st.set_page_config(page_title="Glucose dashboard — Export", page_icon="📊", layout="wide")
|
||||
require_auth()
|
||||
|
||||
st.title("Export")
|
||||
st.caption("Stel een PDF-rapport samen om naar het diabetesteam te sturen (FR10).")
|
||||
|
||||
start, end, days = select_period_days(default_label="90 dagen")
|
||||
sections = st.multiselect("Onderdelen", AVAILABLE_SECTIONS, default=AVAILABLE_SECTIONS)
|
||||
|
||||
try:
|
||||
glucose, _treatments = load_range_data(start, end)
|
||||
except NightscoutConfigError as exc:
|
||||
st.error(str(exc))
|
||||
st.stop()
|
||||
except (requests.RequestException, ValueError) as exc:
|
||||
st.error(f"Kon geen data ophalen van Nightscout: {exc}")
|
||||
st.stop()
|
||||
|
||||
if glucose.empty:
|
||||
st.info("Geen glucosemetingen gevonden in deze periode.")
|
||||
st.stop()
|
||||
|
||||
events_summary = {}
|
||||
if SECTION_EVENTS in sections:
|
||||
levels = {
|
||||
f"Hypo Level 1 (<{LOW})": (LOW, "below"),
|
||||
f"Hypo Level 2 (<{VERY_LOW})": (VERY_LOW, "below"),
|
||||
f"Hyper Level 1 (>{HIGH})": (HIGH, "above"),
|
||||
f"Hyper Level 2 (>{VERY_HIGH})": (VERY_HIGH, "above"),
|
||||
}
|
||||
for label, (threshold, direction) in levels.items():
|
||||
events = detect_events(glucose, threshold, direction)
|
||||
events_summary[label] = summarize_events(events, days)
|
||||
|
||||
pdf_bytes = build_report_pdf(sections, glucose, events_summary, start, end)
|
||||
|
||||
st.download_button(
|
||||
"Download PDF",
|
||||
data=pdf_bytes,
|
||||
file_name=f"glucoserapport_{start.strftime('%Y%m%d')}_{end.strftime('%Y%m%d')}.pdf",
|
||||
mime="application/pdf",
|
||||
)
|
||||
st.caption(f"Periode: {start.strftime('%d-%m-%Y')} t/m {end.strftime('%d-%m-%Y')}")
|
||||
Reference in New Issue
Block a user