A complete Python-based medical form generation system for GHL white-label healthcare businesses. Generates dynamic, responsive medical screening forms from JSON data with advanced conditional logic, BMI calculation, and comprehensive API integration.
π Quick Start
Installation
cd python-forms
pip install -r requirements.txt
python3 regenerate_complete_form.py
This will generate surveys/weightloss/GLP1-screener-live.html
- a complete medical screening form ready for testing.
π Project Structure
python-forms/
βββ enhanced_form_generator.py # Core medical form generation engine
βββ form_data_loader.py # JSON data loader from local directories
βββ regenerate_complete_form.py # Form regeneration script
βββ ../surveys/all-forms/ # Shared sections (Patient Profile, Medical History, etc.)
β βββ patient-profile.json
β βββ medical-history.json
β βββ verification.json
βββ SHOW_CONDITION_PATTERNS.md # Conditional logic documentation
βββ README.md # This file
π§ Core Components
Advanced medical form generator with complete healthcare-specific functionality.
Key Features:
- Multi-section forms: Patient Profile, Medical History, Verification, Assessment
- Conditional logic: Dynamic question visibility (pregnancy, allergies, tobacco follow-ups)
- BMI calculation: Real-time BMI calculation with form-specific disqualification rules
- Phone validation: 10-digit validation with auto-formatting
(555) 123-4567
- Modern UI: Black/white theme with professional styling
- Local storage: Form persistence across browser sessions
- State selector: Full US states dropdown with sync-only state detection
- File uploads: Government ID and photo uploads with validation
- Disqualification logic: Smart messaging for medical disqualification criteria
- API integration: Complete webhook data submission to n8n
Usage:
from enhanced_form_generator import EnhancedFormGenerator
from form_data_loader import FormDataLoader
# Load form data from JSON files
loader = FormDataLoader()
form_data = loader.generate_complete_form_data("Weightloss", "GLP1", "async")
# Generate complete form
generator = EnhancedFormGenerator()
html = generator.generate_notion_form(form_data)
# Save to file
with open("medical_form.html", "w") as f:
f.write(html)
Intelligent JSON parser that combines general sections with form-specific assessments.
Features:
- Dynamic loading: Reads from local
../surveys/all-forms/
and ../surveys/{category}/
directories
- Question ordering: Proper sorting by
property_order
field
- Option sorting: Places βnoneβ, βnone_of_the_aboveβ, βnoβ options last
- ID generation: Creates unique question IDs from Notion data
- Section combination: Merges general sections with form-specific questions
Supported Sections:
- Patient Profile: Name, email, phone, DOB, gender, height/weight
- Medical History: Exercise, allergies, tobacco use, medications
- Verification: Address, ID upload
- Assessment: Form-specific medical questions and conditions
3. Conditional Logic System
Advanced conditional logic for medical forms with comprehensive patterns.
Available Conditions:
always
: Question always visible
if_gender_female
: Shows for female patients (pregnancy questions)
if_allergies_yes
: Shows when allergies are indicated
if_other_glp1s_yes
: Shows for patients taking other GLP-1 medications
if_tobacco_yes
/ if_tobacco_use_yes
: Shows tobacco follow-up questions
Implementation:
// Example: Pregnancy question only for females
{
"property_show_condition": "if_gender_female",
"property_question_text": "Are you currently pregnant or breastfeeding?"
}
- 4 sections: Patient Profile β Medical History β Verification β Assessment
- 30 total questions: Comprehensive medical screening
- Progress tracking: Visual progress bar with section indicators
- Navigation: Previous/Next buttons with proper validation
- Mobile responsive: Works perfectly on all devices
Medical-Specific Features
- BMI calculation: Automatic calculation displayed in Patient Profile
- Disqualification logic: Smart disqualification with clear messaging
- Phone formatting: Real-time formatting with 10-digit validation
- Date validation: Age verification for medical compliance
- File uploads: Secure government ID and photo upload handling
API Integration
- Webhook URL:
https://locumtele.app.n8n.cloud/webhook/patient-screener
- Complete data structure: Contact, patient, form, clinic information
- Question text keys: Answers use readable question text instead of IDs
- Address handling: Full address in contact section
- State-based routing: Automatic sync/async consultation type determination
Data Structure Sent to API
{
"contact": {
"name": "Patient Name",
"email": "patient@email.com",
"phone": "(555) 123-4567",
"address": "123 Main St",
"address2": "Apt 1",
"city": "City",
"state": "CA",
"postalCode": "12345",
"type": "patient"
},
"patient": {
"rxRequested": "GLP1",
"height": "5'10\"",
"weight": "180",
"BMI": "25.8"
},
"form": {
"formType": "screener",
"category": "Weightloss",
"screener": "GLP1",
"timestamp": "2025-01-21T...",
"formVersion": "1.0",
"answers": {
"What is your exercise level?": "moderate",
"Do you have any allergies?": "no",
"Do you currently use tobacco or vape?": "no"
},
"consultType": "async"
},
"clinic": {
"name": "",
"id": "",
"type": "healthcare"
}
}
π― Current Implementation Status
β
Completed Features
- Phone validation: 10-digit validation with auto-formatting
- BMI calculation: Real-time calculation in Patient Profile section
- Conditional logic: All major medical conditional patterns
- Modern styling: Professional black/white theme with gradient accents
- API integration: Complete webhook data submission
- Form persistence: Local storage with intelligent cleanup
- State selector: Full US states with sync-only detection
- Radio button fixes: No pre-selection on fresh loads
- Disqualification styling: Modern, professional messaging
- Tobacco follow-up: Proper conditional logic for tobacco questions
- Total Questions: 30
- Sections: 4 (Patient Profile, Medical History, Verification, Assessment)
- Conditional Questions: 3 (pregnancy, allergy details, tobacco follow-up)
- Disqualifying Questions: 8 with smart messaging
- File Uploads: 2 (government ID, body photos)
π§ͺ Testing Checklist
- β
Phone number formatting and validation
- β
Female gender β pregnancy question appears
- β
Height/weight β BMI calculates automatically
- β
BMI under 25 β can proceed to see value in Assessment
- β
Tobacco βyesβ β follow-up question appears
- β
Disqualifying answers β modern styled messages appear
- β
Form submission β complete data sent to webhook
- β
State selection β proper async/sync routing
- β
File uploads β validation and visual feedback
- β
Form persistence β remembers progress across sessions
π Development Workflow
Making Changes
- Update JSON data: Modify files in
../surveys/all-forms/
or ../surveys/{category}/
- Update generator: Modify
enhanced_form_generator.py
for UI/logic changes
- Regenerate form: Run
python3 regenerate_complete_form.py
- Test: Open generated HTML file in browser
Adding New Conditional Logic
- Update JSON: Add
property_show_condition
to question
- Update JavaScript: Add condition to
checkQuestionVisibility()
function
- Document: Update
SHOW_CONDITION_PATTERNS.md
- Create JSON: Add new file to
../surveys/{category}/
directory
- Update loader: Modify
form_data_loader.py
if needed
- Generate: Use existing generation system
π Deployment
For GHL Integration
- Generate form:
python3 regenerate_complete_form.py
- Extract HTML: Copy content from generated
.html
file
- Embed in GHL: Use as custom HTML widget
- Test webhook: Verify data reaches n8n endpoint
For Client Deployment (Recommended)
- Open Forms Dashboard:
forms-dashboard.html
- Navigate to Dashboard tab: Embed Code Library
- Copy embed code: Click βπ Copyβ button for desired form
- Paste in client website: Embed code is ready for deployment
Local Development
# Generate and test form
python3 regenerate_complete_form.py
open surveys/weightloss/GLP1-screener-live.html
# Monitor changes
# Edit files β regenerate β test β repeat
π Documentation Files
SHOW_CONDITION_PATTERNS.md
: Complete conditional logic documentation
README.md
: This comprehensive guide
- Form generator comments: Inline documentation in code
- JSON examples: Sample data structures in
../surveys/all-forms/
and ../surveys/
directories
π§ Configuration
Key Settings
- Webhook URL:
https://locumtele.app.n8n.cloud/webhook/patient-screener
- Sync-only states: Hardcoded list in form generator
- BMI disqualification: Only applies to GLP1 forms (< 25 BMI)
- Local storage key:
medicalForm_{form_name}_{category}
Customization Points
- Styling: CSS variables in form generator
- Validation rules: JavaScript validation functions
- API structure: Webhook data building function
- Question types: Form input generation methods
π€ Maintenance
Regular Tasks
- JSON updates: Keep form questions current with medical requirements
- Validation updates: Update phone/email validation as needed
- Style updates: Maintain modern, professional appearance
- API monitoring: Ensure webhook integration stays functional
Troubleshooting
- Radio pre-selection: Clear localStorage if issues persist
- Phone formatting: Check
formatPhoneInput()
function
- Conditional logic: Verify question text matching in JavaScript
- API data: Check browser console for webhook submission errors
π License
This project is part of the LocumTele GHL widget system for white-label healthcare businesses.