multiclinic / book_appointment.py
yougandar's picture
Create book_appointment.py
26b0a76 verified
import gradio as gr
import csv
import os
# Hard-coded file path for appointment data
appointment_file_path = "appointments.csv"
patient_file_path = "patients.csv"
# Function to get patient details by ID
def get_patient_details(patient_id):
with open(patient_file_path, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
if row["Patient ID"] == patient_id:
return row["Name"]
return ""
# Function to get doctor names based on selected speciality
def get_doctors_by_speciality(speciality):
doctors = {
"ORTHOPEDICS": ["DR. UDAY KUMAR", "DR. SINDHU", "DR. MURALI"],
"CARDIOLOGY": ["DR. RAVI", "DR. RAMU", "DR. KRISHNA"],
"GENERAL MEDICINES": ["DR. SURESH"],
"NEPHROLOGY": ["DR. GIRISH", "DR. NAVESH"],
"NEUROLOGY": ["DR. DURGESH"]
}
return doctors.get(speciality, [])
# Function to book an appointment
def book_appointment_ui(patient_id, speciality, doctor_name, appointment_date, appointment_time):
patient_name = get_patient_details(patient_id)
if patient_name:
with open(appointment_file_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([patient_id, patient_name, speciality, doctor_name, appointment_date, appointment_time])
return f"Appointment for {patient_name} with {doctor_name} on {appointment_date} at {appointment_time} booked successfully!"
else:
return "Patient ID not found."
# Gradio UI for Book Appointment
def book_appointment_interface(patient_id, speciality, doctor_name, appointment_date, appointment_time):
patient_name = get_patient_details(patient_id)
return book_appointment_ui(patient_id, speciality, doctor_name, appointment_date, appointment_time), patient_name
def update_doctor_dropdown(speciality):
return gr.update(choices=get_doctors_by_speciality(speciality))
app = gr.Blocks()
with app:
with gr.Tab("Book Appointment"):
patient_id = gr.Textbox(label="Patient ID", placeholder="Enter Patient ID", lines=1)
patient_name = gr.Textbox(label="Patient Name", interactive=False, lines=1)
speciality = gr.Dropdown(label="Speciality", choices=["ORTHOPEDICS", "CARDIOLOGY", "GENERAL MEDICINES", "NEPHROLOGY", "NEUROLOGY"])
doctor_name = gr.Dropdown(label="Doctor Name", choices=[])
# Use HTML for date and time selection
appointment_date = gr.HTML("""
<input type="date" id="appointment_date" name="appointment_date">
""")
appointment_time = gr.HTML("""
<input type="time" id="appointment_time" name="appointment_time">
""")
speciality.change(update_doctor_dropdown, inputs=speciality, outputs=doctor_name)
patient_id.change(lambda pid: get_patient_details(pid), inputs=patient_id, outputs=patient_name)
appointment_output = gr.Textbox(label="Appointment Status", lines=1)
gr.Button("Book Appointment").click(book_appointment_interface, inputs=[patient_id, speciality, doctor_name, appointment_date, appointment_time], outputs=[appointment_output, patient_name])
app.launch()