lims / app.py
neerajkalyank's picture
Update app.py
31ade35 verified
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": 0, "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
if last_sequence["year"] != current_year or last_sequence["month"] != current_month:
last_sequence["year"] = current_year
last_sequence["month"] = current_month
last_sequence["number"] = 1
else:
last_sequence["number"] += 1
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, gender, phone, address):
patients, last_sequence = load_data()
patient_id = generate_patient_id(phone, last_sequence)
patients[patient_id] = {
"name": name,
"father_name": father_name,
"age": age,
"gender": gender, # Updated field for gender
"phone": phone,
"address": address,
"tests": [],
"total_cost": 0
}
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)
def confirm_tests_interface(patient_id, selected_tests):
patients, last_sequence = load_data()
response = select_tests(patient_id, selected_tests, patients)
save_data(patients, last_sequence)
return response
# Billing Tab
def billing_interface(patient_id):
patients, _ = load_data()
if patient_id in patients:
billing_info = fetch_billing(patient_id, patients)
return billing_info
else:
return "Invalid Patient ID. Please check the ID."
# Custom CSS styling
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
/* Reset */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* General Page Style */
body {
font-family: 'Roboto', sans-serif;
background: #f4f7f9;
color: #333;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Main Container */
.container {
max-width: 800px;
width: 100%;
margin: 30px auto;
padding: 40px;
background-color: #fff;
border-radius: 12px;
border: 1px solid #e3e8ec;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.container:hover {
transform: scale(1.01);
box-shadow: 0 12px 28px rgba(0, 0, 0, 0.15);
}
/* Header Styling */
#header {
color: #1c3f60;
text-align: center;
font-weight: 700;
font-size: 2.5em;
margin-bottom: 30px;
letter-spacing: 0.6px;
text-transform: uppercase;
}
/* Form Field Styles */
input[type="text"], input[type="email"], select, textarea {
width: 100%;
border: 1px solid #d1d9e0;
border-radius: 8px;
padding: 14px;
font-size: 16px;
color: #495057;
background: #f9fbfc;
margin-bottom: 20px;
transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
input:focus, select:focus, textarea:focus {
border-color: #1c7ed6;
box-shadow: 0 4px 12px rgba(28, 126, 214, 0.2);
outline: none;
}
/* Button Styles */
button {
display: inline-block;
padding: 12px 24px;
font-size: 16px;
font-weight: 600;
color: #ffffff;
background-color: #1c7ed6;
border: none;
border-radius: 8px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
box-shadow: 0 5px 15px rgba(28, 126, 214, 0.3);
}
button:hover {
background-color: #155a9a;
transform: translateY(-2px);
box-shadow: 0 8px 24px rgba(21, 90, 154, 0.3);
}
button:active {
transform: translateY(1px);
}
/* Toggle Switch */
.toggle-switch {
position: relative;
width: 60px;
height: 34px;
display: inline-block;
}
.toggle-switch input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #cfd8e3;
transition: 0.4s;
border-radius: 34px;
}
.toggle-slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: 0.4s;
border-radius: 50%;
}
input:checked + .toggle-slider {
background-color: #1c7ed6;
}
input:checked + .toggle-slider:before {
transform: translateX(26px);
}
/* Checkbox Styles */
.checkbox-container {
display: flex;
align-items: center;
margin-bottom: 20px;
cursor: pointer;
}
.checkbox-container input[type="checkbox"] {
opacity: 0;
position: absolute;
}
.custom-checkbox {
width: 20px;
height: 20px;
background-color: #cfd8e3;
border-radius: 4px;
transition: background-color 0.3s;
display: flex;
align-items: center;
justify-content: center;
}
.checkbox-container input[type="checkbox"]:checked + .custom-checkbox {
background-color: #1c7ed6;
}
.checkbox-container input[type="checkbox"]:checked + .custom-checkbox::after {
content: "✓";
color: white;
font-size: 14px;
}
/* Radio Button Group */
.radio-group {
display: flex;
gap: 12px;
margin-bottom: 20px;
}
.radio-group label {
display: flex;
align-items: center;
padding: 10px 20px;
border: 1px solid #d1d9e0;
border-radius: 8px;
font-size: 16px;
color: #333;
background-color: #f9fbfc;
cursor: pointer;
transition: border-color 0.3s, background-color 0.3s;
}
.radio-group input[type="radio"] {
display: none;
}
.radio-group input[type="radio"]:checked + label {
background-color: #e9f2fd;
border-color: #1c7ed6;
color: #1c7ed6;
}
/* Tabs */
.tabs {
display: flex;
justify-content: space-around;
background-color: #f4f7f9;
padding: 10px;
border-bottom: 2px solid #e3e8ec;
margin-bottom: 25px;
border-radius: 8px;
}
.tabs .tab-item {
font-size: 1.1em;
font-weight: 500;
color: #6c757d;
padding: 10px 20px;
border-radius: 6px;
cursor: pointer;
transition: color 0.3s, background-color 0.3s;
}
.tabs .tab-item:hover, .tabs .tab-item.active {
color: #1c7ed6;
background-color: #e9f2fd;
}
/* Output Section */
.output-box {
padding: 18px;
background-color: #f4f7f9;
border: 1px solid #e3e8ec;
border-radius: 8px;
color: #333;
font-weight: 500;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
transition: background-color 0.3s, border-color 0.3s;
}
.output-box:hover {
background-color: #edf2f7;
border-color: #1c7ed6;
}
"""
# Gradio Interface
with gr.Blocks(css=custom_css) as app:
gr.Markdown("<h1 style='text-align: center;'>👩🏻‍🔬丂卂ㄒ卄Ҝ尺ㄩㄒ卄卂 ㄥ丨爪丂🔬</h1>", elem_id="header")
with gr.Tab("Patient Registration"):
gr.Markdown("<h2>Register a New Patient</h2>")
with gr.Row():
name = gr.Textbox(label="Patient Name", placeholder="Enter patient's full name", elem_id="name_field")
father_name = gr.Textbox(label="Father/Husband's Name", placeholder="Enter father/husband's full name", elem_id="father_name_field")
with gr.Row():
age = gr.Number(label="Age", value=None, minimum=0, maximum=120, elem_id="age_field")
gender = gr.Radio(
choices=["Male", "Female", "Other"],
label="Gender",
elem_id="gender_field"
)
with gr.Row():
phone = gr.Textbox(label="Phone Number", placeholder="Enter a valid 10-digit phone number", elem_id="phone_field")
address = gr.Textbox(label="Address", placeholder="Enter full address", lines=2, elem_id="address_field")
register_button = gr.Button("Register Patient", elem_id="register_button")
registration_output = gr.Markdown(label="Registration Output", elem_id="registration_output")
register_button.click(registration_interface, [name, father_name, age, gender, phone, address], registration_output)
with gr.Tab("Tests"):
gr.Markdown("<h2>Select Tests for the Patient</h2>")
patient_id_test = gr.Textbox(label="Patient ID", placeholder="Enter 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 Selected Tests")
# Use Markdown for Test Selection Output to allow for auto-expanding content
test_output = gr.Markdown(label="Test Selection Output", elem_id="test_output")
categories.change(test_interface, inputs=categories, outputs=available_tests)
confirm_button.click(confirm_tests_interface, [patient_id_test, available_tests], test_output)
with gr.Tab("Billing"):
gr.Markdown("<h2>Billing Information</h2>")
patient_id_bill = gr.Textbox(label="Patient ID", placeholder="Enter Patient ID for Billing")
fetch_button = gr.Button("Fetch Billing Details")
# Use Markdown for Billing Output to allow for auto-expanding content
billing_output = gr.Markdown(label="Billing Information", elem_id="billing_output")
fetch_button.click(billing_interface, [patient_id_bill], billing_output)
app.launch()