Skip to main content

Python flask APIs

 

from flask import Flask, request, jsonify from flask_sqlalchemy import SQLAlchemy from datetime import datetime from sqlalchemy import and_ app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///patient_data.db' # Use SQLite for simplicity app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class PatientData(db.Model): id = db.Column(db.Integer, primary_key=True) patient_id = db.Column(db.String(50), nullable=False) timestamp = db.Column(db.DateTime, nullable=False) temperature = db.Column(db.Float) blood_pressure = db.Column(db.String(20)) heart_rate = db.Column(db.Integer) oxygen_saturation = db.Column(db.Integer) ecg = db.Column(db.String(20)) ppg = db.Column(db.String(20)) def to_dict(self): return { "timestamp": self.timestamp.isoformat(), "temperature": self.temperature, "blood_pressure": self.blood_pressure, "heart_rate": self.heart_rate, "oxygen_saturation": self.oxygen_saturation, "ecg": self.ecg, "ppg": self.ppg } @app.route('/api/patient_data', methods=['GET']) def get_patient_data(): patient_id = request.args.get('patient_id') start_time = request.args.get('start_time') end_time = request.args.get('end_time') # Validate input parameters if not all([patient_id, start_time, end_time]): return jsonify({"error": "Missing required parameters"}), 400 try: start_time = datetime.fromisoformat(start_time) end_time = datetime.fromisoformat(end_time) except ValueError: return jsonify({"error": "Invalid datetime format. Use ISO format (YYYY-MM-DDTHH:MM:SS)"}), 400 if start_time > end_time: return jsonify({"error": "Start time must be before end time"}), 400 # Query the database for patient data patient_data = PatientData.query.filter( and_( PatientData.patient_id == patient_id, PatientData.timestamp >= start_time, PatientData.timestamp <= end_time ) ).order_by(PatientData.timestamp).all() return jsonify({ "patient_id": patient_id, "start_time": start_time.isoformat(), "end_time": end_time.isoformat(), "data": [data.to_dict() for data in patient_data] }) @app.route('/api/add_patient_data', methods=['POST']) def add_patient_data(): data = request.json new_data = PatientData( patient_id=data['patient_id'], timestamp=datetime.fromisoformat(data['timestamp']), temperature=data['temperature'], blood_pressure=data['blood_pressure'], heart_rate=data['heart_rate'], oxygen_saturation=data['oxygen_saturation'], ecg=data['ecg'], ppg=data['ppg'] ) db.session.add(new_data) db.session.commit() return jsonify({"message": "Data added successfully"}), 201 if __name__ == '__main__': with app.app_context(): db.create_all() # Create database tables app.run(debug=True)

Comments

Popular posts from this blog

Telecom OSS and BSS: A Comprehensive Guide

  Telecom OSS and BSS: A Comprehensive Guide Table of Contents Part I: Foundations of Telecom Operations Chapter 1: Introduction to Telecommunications Networks A Brief History of Telecommunications Network Architectures: From PSTN to 5G Key Network Elements and Protocols Chapter 2: Understanding OSS and BSS Defining OSS and BSS The Role of OSS in Network Management The Role of BSS in Business Operations The Interdependence of OSS and BSS Chapter 3: The Telecom Business Landscape Service Providers and Their Business Models The Evolving Customer Experience Regulatory and Compliance Considerations The Impact of Digital Transformation Part II: Operations Support Systems (OSS) Chapter 4: Network Inventory Management (NIM) The Importance of Accurate Inventory NIM Systems and Their Functionality Data Modeling and Management Automation and Reconciliation Chapter 5: Fault Management (FM) Detecting and Isolating Network Faults FM Systems and Alerting Mecha...

"Depth-Guard" – 3D Spatial Occupancy monitor Challenge -2

  Project Title: "Depth-Guard" – 3D Spatial Occupancy Monitor 1. The Problem In a smart warehouse, a robot needs to know if a loading zone is clear or occupied. A 2D camera alone can’t tell the difference between a "flat picture of a box" on the floor and an "actual 3D box." The Goal: Build a Python-based system that uses Computer Vision and Depth Perception (AI 3D) to identify objects and determine their 3D volume (Size) and Distance from the camera. 2. Intern Tasks Object Detection: Use a pre-trained model (like YOLOv8) to draw 2D boxes around objects. Depth Mapping: Use a depth estimation model (like MiDaS or a simulated Stereo-depth feed) to calculate how far each object is. Occupancy Logic: If an object is closer than 1 meter and larger than a specific volume, mark the zone as "BLOCKED." Alert System: Print a warning if the 3D space is too crowded. 3. Sample Datasets (Simulation) Since interns may not have 3D cameras (LiDAR/RGB-D), pr...

Simple Virtual Waiting Room -Challenge 1

   Simple Virtual Waiting Room (VWR) 1. The Problem Our website can only handle 10 users per minute . If more than 10 people try to access it at once, the server will crash. We need a system that: Counts incoming users. Redirects "overflow" users to a waiting page. Admits them back to the main site one by one as space becomes available. 2. Intern Tasks Create a Gateway: A simple script that checks: if (active_users < 10) { allow } else { send to queue } . Build the Queue: Use a simple list (FIFO) to store user IDs. The Wait Page: A basic HTML page that says: "You are number X in line. Estimated wait: Y minutes." Admission Logic: Every 30 seconds, pull the next user from the queue and "admit" them. 3. Sample Datasets (Simulation) Provide these two datasets to the interns. They should write a script to "read" these files and simulate how their system reacts. Dataset A: The Traffic Surge (Input) This file simulates users arriving at the ...