SPECIAL / view_appointments.py
yougandar's picture
Update view_appointments.py
2ddd95c verified
import gradio as gr
import pandas as pd
import os
import csv
# File paths
appointments_file_path = "appointments.csv"
patients_file_path = "patients.csv"
def initialize_csv(file_path, headers):
"""Initialize the CSV file with headers if it doesn't exist."""
if not os.path.exists(file_path):
with open(file_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headers)
# Initialize the CSV files with headers
initialize_csv(appointments_file_path, ["Patient ID", "Patient Name", "Doctor Name", "Appointment Date", "Appointment Time"])
initialize_csv(patients_file_path, ["Patient ID", "Patient Name", "Age", "Gender", "Contact Number"])
def get_patient_details(patient_id):
"""Get patient details by Patient ID."""
patient_id = patient_id.strip()
with open(patients_file_path, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
if row["Patient ID"].strip() == patient_id:
return row["Patient Name"]
return ""
def get_appointment_details(patient_id):
"""Retrieve appointment details for a given Patient ID."""
patient_id = str(patient_id).strip()
if not os.path.exists(appointments_file_path):
return "Appointments file not found."
try:
appointments_df = pd.read_csv(appointments_file_path)
except pd.errors.EmptyDataError:
return "Appointments file is empty."
except pd.errors.ParserError:
return "Error parsing the appointments file."
# Ensure that Patient ID is properly matched
appointment = appointments_df[appointments_df["Patient ID"].astype(str).str.strip() == patient_id]
if appointment.empty:
return f"No appointment records found for Patient ID: {patient_id}."
details = []
for _, row in appointment.iterrows():
detail = (
f"Patient Name: {row['Patient Name']}\n"
f"Doctor Name: {row['Doctor Name']}\n"
f"Appointment Date: {row['Appointment Date']}\n"
f"Appointment Time: {row['Appointment Time']}\n"
)
details.append(detail)
return "\n\n".join(details)
def gradio_interface():
"""Create Gradio interface for viewing appointment details."""
with gr.Blocks() as demo:
with gr.Row():
with gr.Column(scale=1): # Make this column smaller for Patient ID input
# Input for patient ID
patient_id_input = gr.Number(label="Patient ID", precision=0)
with gr.Column(scale=3): # Larger column for the outputs
# Auto-generated output for patient name
patient_name_output = gr.Textbox(label="Patient Name", interactive=False)
# Output for appointment details
appointment_output = gr.Textbox(label="Appointment Details", interactive=False, lines=15)
def update_details(patient_id):
patient_name = get_patient_details(str(patient_id))
appointment_details = get_appointment_details(str(patient_id))
return patient_name, appointment_details
# Update patient name and appointment details when Patient ID is entered
patient_id_input.change(
fn=update_details,
inputs=patient_id_input,
outputs=[patient_name_output, appointment_output]
)
return demo
# Create the Gradio interface
demo = gradio_interface()
# Launch the Gradio app
if __name__ == "__main__":
demo.launch()