from flask import Flask, request, jsonify, render_template_string import sqlite3 import datetime app = Flask(__name__) DB_NAME = "hospital.db" def init_db(): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS patients (bed_id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT, disease TEXT)''') c.execute('''CREATE TABLE IF NOT EXISTS vitals (id INTEGER PRIMARY KEY AUTOINCREMENT, bed_id INTEGER, bpm REAL, spo2 REAL, temp REAL, ecg TEXT, timestamp TEXT)''') conn.commit() conn.close() init_db() # --- MAIN DASHBOARD TEMPLATE --- HTML_TEMPLATE = ''' Hospital Ward Control Dashboard

🏥 Autonomous Medical Assistant - Ward Dashboard

Assign Patient to Bed

Bed No: Name: Age: Gen: Disease:

Active Patients in Ward

{% for p in patients %} {% endfor %} {% if not patients %} {% endif %}
Bed NoPatient NameAgeGenderDiagnosed DiseaseActions
Bed {{ p[0] }}{{ p[1] }}{{ p[2] }}{{ p[3] }}{{ p[4] }} View Report Discharge
No active patients in the ward.
''' # --- PATIENT REPORT TEMPLATE (NOW WITH CHART.JS FOR ECG) --- REPORT_TEMPLATE = ''' Patient Report - Bed {{ bed_id }}
⬅ Back to Dashboard

Patient Portfolio - Bed {{ bed_id }}

Name: {{ patient[1] }}  |  Age: {{ patient[2] }}  |  Gender: {{ patient[3] }}

Diagnosis: {{ patient[4] }}

Vitals History

{% for v in vitals %} {% if v[5] != '[0]' and v[5] != '0' %} {% endif %} {% endfor %} {% if not vitals %} {% endif %}
TimestampHeart RateSpO2 LevelTemperatureECG Trace
{{ v[6] }} {% if v[2] > 0 %}{{ v[2] }} BPM{% else %}None{% endif %} {% if v[3] > 0 %}{{ v[3] }} %{% else %}None{% endif %} {% if v[4] > 0 %}{{ v[4] }} °C{% else %}None{% endif %} {% if v[5] == '[0]' or v[5] == '0' %} Not Recorded {% else %} {% endif %}
No vitals recorded yet.
''' @app.route('/') def index(): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("SELECT * FROM patients ORDER BY bed_id ASC") patients = c.fetchall() conn.close() return render_template_string(HTML_TEMPLATE, patients=patients) @app.route('/report/') def view_report(bed_id): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("SELECT * FROM patients WHERE bed_id=?", (bed_id,)) patient = c.fetchone() if not patient: conn.close() return "Patient not found. They might have been discharged.", 404 c.execute("SELECT * FROM vitals WHERE bed_id=? ORDER BY timestamp DESC", (bed_id,)) vitals = c.fetchall() conn.close() return render_template_string(REPORT_TEMPLATE, patient=patient, vitals=vitals, bed_id=bed_id) @app.route('/add_patient', methods=['POST']) def add_patient(): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("REPLACE INTO patients (bed_id, name, age, gender, disease) VALUES (?, ?, ?, ?, ?)", (request.form['bed_id'], request.form['name'][:15], request.form['age'], request.form['gender'], request.form['disease'][:15])) conn.commit() conn.close() return index() @app.route('/remove/') def remove_patient(bed_id): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("DELETE FROM patients WHERE bed_id=?", (bed_id,)) c.execute("DELETE FROM vitals WHERE bed_id=?", (bed_id,)) conn.commit() conn.close() return index() @app.route('/api/system_status', methods=['GET']) def get_status(): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("SELECT COUNT(*) FROM patients") count = c.fetchone()[0] conn.close() return jsonify({"total_patients": count}) @app.route('/api/patient/', methods=['GET']) def get_patient(bed_id): conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("SELECT name, age, gender, disease FROM patients WHERE bed_id=?", (bed_id,)) row = c.fetchone() conn.close() if row: return jsonify({"found": True, "name": row[0], "age": row[1], "gender": row[2], "disease": row[3]}) return jsonify({"found": False}) @app.route('/api/vitals', methods=['POST']) def save_vitals(): data = request.json timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") conn = sqlite3.connect(DB_NAME) c = conn.cursor() c.execute("INSERT INTO vitals (bed_id, bpm, spo2, temp, ecg, timestamp) VALUES (?, ?, ?, ?, ?, ?)", (data['bed_id'], data['bpm'], data['spo2'], data['temp'], str(data['ecg']), timestamp)) conn.commit() conn.close() return jsonify({"status": "success"}) if __name__ == '__main__': app.run(host='0.0.0.0', port=7860)