lims / app.py
neerajkalyank's picture
Update app.py
bbf8b65 verified
raw
history blame
8.84 kB
import gradio as gr
import json
from datetime import datetime
from patient_registration import register_patient
from test_selection import select_tests, get_tests_by_category
from billing import fetch_billing
# Load data
def load_data():
try:
with open("data.json", "r") as file:
data = json.load(file)
return data.get("patients", {}), data.get("last_sequence", {"year": None, "month": None, "number": 0})
except FileNotFoundError:
return {}, {"year": None, "month": None, "number": 0}
# Save data
def save_data(patients, last_sequence):
with open("data.json", "w") as file:
json.dump({"patients": patients, "last_sequence": last_sequence}, file)
# Generate patient ID
def generate_patient_id(phone, last_sequence):
today = datetime.now()
current_year = today.year
current_month = today.month
# Check if year or month has changed
if last_sequence["year"] != current_year or last_sequence["month"] != current_month:
# Reset sequence for new year/month
last_sequence["year"] = current_year
last_sequence["month"] = current_month
last_sequence["number"] = 1
else:
# Increment sequence number
last_sequence["number"] += 1
# Format patient ID
patient_id = f"{current_year}{current_month:02d}{last_sequence['number']:05d}"
return patient_id
# Patient Registration Tab
def registration_interface(name, father_name, age, phone, address, pincode):
patients, last_sequence = load_data()
# Generate patient ID
patient_id = generate_patient_id(phone, last_sequence)
# Add patient details to data
patients[patient_id] = {
"name": name,
"father_name": father_name,
"age": age,
"phone": phone,
"address": address,
"pincode": pincode,
"tests": [],
"total_cost": 0
}
# Save data and updated sequence
save_data(patients, last_sequence)
return f"Patient Registered. Patient ID: {patient_id}"
# Tests Selection Tab
def test_interface(categories):
available_tests = get_tests_by_category(categories)
return gr.update(choices=available_tests) # Update dropdown with available tests
def confirm_tests_interface(patient_id, selected_tests):
patients, last_sequence = load_data() # Load both patients and last_sequence
response = select_tests(patient_id, selected_tests, patients) # Update patients directly
save_data(patients, last_sequence) # Save both patients and last_sequence
return response
# Billing Tab
def billing_interface(patient_id):
patients, _ = load_data() # Load patients and ignore last_sequence
if patient_id in patients:
billing_info = fetch_billing(patient_id, patients)
return billing_info
else:
return "Invalid Patient ID. Please check the ID."
# Gradio Interface
with gr.Blocks() as app:
gr.Markdown("<h1 style='text-align: center;'>πŸ‘©πŸ»β€πŸ”¬πŸ…’πŸ…πŸ…£πŸ…—πŸ…šπŸ…‘πŸ…€πŸ…£πŸ…—πŸ… πŸ…›πŸ…˜πŸ…œπŸ…’πŸ”¬</h1>", elem_id="header")
with gr.Tab("Patient Registration"):
with gr.Row():
name = gr.Textbox(label="Patient Name", placeholder="Enter patient name", elem_id="name_field", lines=1)
father_name = gr.Textbox(label="Father/Husband Name", placeholder="Enter father or husband name", elem_id="father_name_field", lines=1)
with gr.Row():
age = gr.Number(label="Age", value=None, minimum=0, maximum=99, elem_id="age_field")
phone = gr.Number(label="Phone Number", value=None, elem_id="phone_field")
with gr.Row():
address = gr.Textbox(label="Address", placeholder="Enter address", lines=2)
pincode = gr.Number(label="Pincode", value=None, elem_id="pincode_field")
register_button = gr.Button("Register Patient")
registration_output = gr.Textbox(label="Registration Output")
register_button.click(registration_interface, [name, father_name, age, phone, address, pincode], registration_output)
with gr.Tab("Tests"):
patient_id_test = gr.Textbox(label="Patient ID")
categories = gr.CheckboxGroup(
["Haematology", "Clinical Pathology", "Biochemistry", "Microbiology", "Specific Diseases", "Serology", "Radiology", "Other Diagnostic Tests"],
label="Select Test Categories"
)
available_tests = gr.CheckboxGroup(label="Available Tests")
confirm_button = gr.Button("Confirm Tests")
test_output = gr.Textbox(label="Test Selection Output")
categories.change(test_interface, inputs=categories, outputs=available_tests) # Update available tests based on selected categories
confirm_button.click(confirm_tests_interface, [patient_id_test, available_tests], test_output)
with gr.Tab("Billing"):
patient_id_bill = gr.Textbox(label="Patient ID")
fetch_button = gr.Button("Fetch Billing")
billing_output = gr.Textbox(label="Billing Information")
fetch_button.click(billing_interface, [patient_id_bill], billing_output)
# Custom CSS styling
app.css = """
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap');
/* General Page Style */
body {
font-family: 'Poppins', Arial, sans-serif;
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
color: #e0f7fa;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Header */
#header {
color: #4dea27;
text-align: center;
font-weight: 700;
font-size: 2.5em;
margin-bottom: 25px;
text-shadow: 3px 3px 10px rgba(0, 0, 0, 0.5);
letter-spacing: 1px;
}
/* Hover Effect for Header */
#header:hover {
color: #ff8c00; /* Change to a different shade on hover */
transform: scale(1.05); /* Slightly scale up */
}
/* Container Style */
.gradio-container {
max-width: 850px;
margin: 30px auto;
padding: 40px;
background: rgba(50, 50, 50, 0.95);
border-radius: 20px;
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.8);
color: #ffffff;
}
/* Form Field Styles with Strip Effect */
#name_field, #father_name_field, #age_field, #phone_field, #address_field, #pincode_field, textarea {
max-width: 700px;
border: 1px solid #444;
border-radius: 8px;
padding: 12px;
background: rgba(245, 245, 245, 0.85);
color: #2d2d2d;
font-size: 15px;
margin-bottom: 15px;
transition: border 0.3s ease, box-shadow 0.3s ease, transform 0.2s ease;
box-shadow: 0px 4px 0px 0px #d0d0d0; /* Adds the strip effect at the bottom */
}
#name_field:focus, #father_name_field:focus, #age_field:focus, #phone_field:focus, #address_field:focus, #pincode_field:focus, textarea:focus {
border-color: #ffd54f;
box-shadow: 0 0 10px rgba(255, 213, 79, 0.7);
transform: scale(1.02);
}
/* Textarea */
textarea {
font-size: 15px;
padding: 15px;
border-radius: 8px;
background: rgba(245, 245, 245, 0.9);
border: 1px solid #555;
transition: 0.3s ease;
}
/* Button Style */
.gr-button {
color: #ffffff;
background: linear-gradient(135deg, #4a90e2, #007aff);
border: none;
border-radius: 12px;
padding: 12px 30px;
cursor: pointer;
font-weight: 600;
font-size: 16px;
transition: 0.3s ease;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}
.gr-button:hover {
background: linear-gradient(135deg, #005cbf, #0061a7);
transform: scale(1.05);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.5);
}
/* Tab container styling */
.nav-tabs {
display: flex;
justify-content: space-around;
background-color: #333;
border-bottom: 1px solid #fff;
padding: 10px;
}
/* Individual tab styling */
.nav-tabs .nav-item {
font-size: 1em;
color: #ffffff; /* Inactive tabs in white */
padding: 10px 20px;
cursor: pointer;
transition: color 0.3s, transform 0.3s;
font-weight: 500;
}
/* Hover effect on tabs */
.nav-tabs .nav-item:hover {
color: #FFCA28; /* Bright yellow on hover */
}
/* Active tab styling */
.nav-tabs .nav-item.active {
color: #FF8C00; /* Orange color for active tab */
border-bottom: 3px solid #FF8C00; /* Orange underline for active tab */
font-weight: 700;
}
/* Billing Output */
#billing_output {
background: rgba(40, 40, 40, 0.9);
padding: 20px;
border-radius: 8px;
color: #ffeb3b;
font-size: 18px;
font-weight: bold;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}
/* Text and Placeholder Styling */
::placeholder {
color: #888;
}
h1, h2, h3, h4, h5, h6 {
color: #ffeb3b;
font-weight: 700;
text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
}
/* Checkboxes */
.checkbox, .textbox {
margin-bottom: 18px;
color: #f5f5f5;
}
"""
app.launch()