Pregnancy monitoring with risk prediction
Clinical pregnancy monitoring platform with ML-based risk prediction, built on Django and PostgreSQL for a Berlin hospital research institute.
The Challenge
A Berlin hospital research institute (Charité) needed a clinical toolkit for monitoring patient pregnancies and predicting adverse outcomes like preterm delivery. The platform had to meet strict healthcare data protection requirements — including audit trails and pseudonymization of patient data — while remaining accessible to clinicians with varying technical literacy. Data from multiple hospital organizations had to remain strictly isolated, and the system needed to integrate with external ML prediction services for risk analysis during examinations.
The Approach
The project followed a server-rendered progressive enhancement strategy: Django templates handle page structure and routing, while vanilla JavaScript modules add targeted interactivity — no heavyweight SPA framework required. Business logic lives in dedicated service layers, keeping views thin and logic independently testable. The frontend uses Plotly.js for clinical data visualization and Unpoly for partial page updates without full reloads. Django formsets with custom JavaScript handle complex nested forms like multiple birth records, and data attributes drive conditional field visibility declaratively.
Implementation
A core contribution was the risk prediction visualization system. Starting with a basic PredictionGraph component using Plotly.js, this evolved over multiple iterations into a full interactive chart with examination-based data selection, grouped traces for submissions and examinations, tooltips containing data tables, responsive layout, and gestation age validation that prevents queries when values are out of range. The ML integration includes backend validation of required fields before querying the prediction service, proper async job handling via Redis Queue to prevent request hangs, and graceful frontend error handling when ML inputs are invalid.
Multiple pregnancy and twin support was another major feature. This required creating a new Child model with a custom data migration that loops through existing outcome records to migrate data without loss. The frontend uses Django formsets with custom JavaScript to dynamically add and remove child sub-forms, a tab-based UI for switching between children, and proper Django management form count synchronization via JS — solving a subtle issue where Django ignores forms beyond the declared total count.
The organization locking system allows administrators to freeze clinical data. This was implemented across the full stack: a locked flag on the Organization model, model-layer save prevention that blocks creation, alteration, and deletion of patients, examinations, submissions, and outcomes, and frontend logic that hides all action buttons when an organization is locked. Patient personal data remains editable even when locked.
Patient pseudonymization enables deletion of personal data from patient records while preserving the clinical record for research continuity. The implementation allows patients to exist without an account or personal data, with removal triggered on account deletion.
Additional features include an audit log with filtering by patient ID for compliance tracking, examination deletion with a confirmation workflow and e2e test coverage, hospitalization indication fields (inpatient status and admission recommendation), a preterm delivery checkbox for the outcome form, and a full navigation restructuring using Django template tags for consistent URL handling across authentication, landing pages, and clinical views. The Playwright E2E testing migration replaced the legacy pytest-splinter setup with modern locator-based tests running against a live Django server.
Results
The architecture delivers a clinical UI that handles complex medical workflows — from multi-child birth records to interactive risk prediction charts — without SPA complexity. The risk prediction integration gracefully handles ML service failures through async job processing and frontend fallbacks, ensuring clinician workflows aren't blocked by external service issues. Organization locking and pseudonymization provide the data governance controls required in a hospital research setting, while the audit log enables compliance verification. The Playwright migration provides more reliable browser-based regression testing compared to the previous Splinter-based approach.
Highlights
- Interactive ML risk prediction charts
- Multiple pregnancy and twin support
- Organization-level data locking
- Patient pseudonymization on deletion
- Audit logging with patient filtering
- Playwright E2E testing migration