SPECIAL / register_patient.py
yougandar's picture
Update register_patient.py
fa4452f verified
import gradio as gr
import csv
import os
# Define the path to the CSV file
patient_file_path = "patients.csv"
def initialize_csv():
"""Initialize the CSV file if it doesn't exist."""
if not os.path.exists(patient_file_path):
with open(patient_file_path, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Patient ID", "Name", "Age", "Age Unit", "Gender", "Address", "Contact Number"])
def register_patient(name, age, age_unit, gender, contact):
"""Register a new patient and return the registration status."""
address = "Hyderabad"
initialize_csv() # Ensure the CSV is initialized
# Read the last Patient ID and increment it
with open(patient_file_path, mode='r') as file:
reader = csv.reader(file)
rows = list(reader)
last_id = int(rows[-1][0]) if len(rows) > 1 else 0 # Get the last Patient ID
new_id = last_id + 1
# Write the new patient details to the CSV file
with open(patient_file_path, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([new_id, name, age, age_unit, gender, address, contact])
return f"Patient ID: {new_id}, Name: {name} registered successfully."
def create_gradio_interface():
"""Create and return the Gradio interface for patient registration."""
initialize_csv() # Ensure CSV is initialized before launching the interface
with gr.Blocks() as demo:
gr.Markdown("## Register Patient")
with gr.Row():
with gr.Column():
# Define Gradio inputs
name = gr.Textbox(label="Patient Name", placeholder="Enter patient name")
# Age input with number and unit selection similar to gender buttons
age = gr.Number(label="Age", value=0)
age_unit = gr.Radio(["Years", "Months", "Days"], label="Age Unit", value="Years")
contact_number = gr.Textbox(label="Contact Number", placeholder="Enter contact number")
with gr.Column():
# Gender input
gender = gr.Radio(["Male", "Female", "Transgender"], label="Gender")
# Define result output
result = gr.Textbox(label="Result", interactive=False)
# Define Register button
register_button = gr.Button("Register")
# Define the registration logic
register_button.click(
fn=register_patient,
inputs=[name, age, age_unit, gender, contact_number],
outputs=result
)
return demo
if __name__ == "__main__":
create_gradio_interface().launch()