SPECIAL / consultation.py
yougandar's picture
Update consultation.py
84ff0f7 verified
import gradio as gr
import pandas as pd
import csv
import os
# File paths for the CSV files
patients_file_path = 'patients.csv'
consultations_file_path = 'consultations.csv'
# Function to get patient details by ID
def get_patient_details(patient_id):
with open(patients_file_path, mode='r') as file:
reader = csv.DictReader(file)
for row in reader:
if row["Patient ID"] == str(patient_id): # Ensure ID is a string for comparison
return row["Patient Name"] # Updated to match CSV header
return ""
# Function to autofill patient name in the Book Appointment section
def autofill_patient_name(patient_id):
return get_patient_details(patient_id)
# Dummy data for medicines and frequencies
medicine_names = ['Aspirin', 'Ibuprofen', 'Paracetamol', 'Amoxicillin']
frequencies = [
'every other day',
'q.a.m. (every day before noon)',
'q.d. (every day)',
'q.d.s. (four times a day)',
'q.p.m. (every day after noon or every evening)',
'q.h. (every hour)',
'q.h.s. (every night at bedtime)',
'q.1 h (every 1 hour)',
'q.i.d. (four times a day)',
'q4PM (at 4 pm)',
'q.o.d. (every other day)',
'qqh (every four hours)'
]
# Dummy data for lab tests and departments
test_names = ['Complete Blood Count', 'Liver Function Test', 'Kidney Function Test', 'X-Ray']
departments = ['Hematology', 'Biochemistry', 'Radiology', 'Pathology']
# Function to handle form submission and store data in CSV
def submit_consultation(patient_id, height, weight, diastolic_bp, systolic_bp, temperature, chief_complaints, prescription_data, lab_investigations, revisit_date, revisit_time):
patient_name = autofill_patient_name(patient_id)
# Read the existing CSV file to get the latest consultation ID
if os.path.exists(consultations_file_path):
df = pd.read_csv(consultations_file_path)
if not df.empty:
last_id = df["Consultation ID"].max()
else:
last_id = 0
else:
last_id = 0
new_id = last_id + 1 # Increment the last ID for the new record
result = {
"Consultation ID": new_id,
"Patient ID": patient_id,
"Patient Name": patient_name,
"Height (cms)": height,
"Weight (kgs)": weight,
"Diastolic BP": diastolic_bp,
"Systolic BP": systolic_bp,
"Temperature (°F)": temperature, # Updated to Fahrenheit
"Chief Complaints": chief_complaints,
"Prescription": prescription_data,
"Lab Investigations": lab_investigations,
"Revisit Date": revisit_date,
"Revisit Time": revisit_time
}
# Write to CSV
with open(consultations_file_path, mode='a', newline='') as file:
writer = csv.DictWriter(file, fieldnames=result.keys())
# Write header if file is new
if os.stat(consultations_file_path).st_size == 0:
writer.writeheader()
writer.writerow(result)
return "Consultation details submitted successfully."
# Function to handle prescription data
def add_drug(drug_name, frequency, quantity, existing_prescriptions):
# Add a row number or ID based on the current length of the list
row_number = len(existing_prescriptions) + 1
new_prescription = {
"Row Number": row_number,
"Drug Name": drug_name,
"Frequency": frequency,
"Quantity": quantity
}
existing_prescriptions.append(new_prescription)
return existing_prescriptions
# Function to handle lab investigations
def add_lab_test(test_name, department, existing_tests):
# Add a row number or ID based on the current length of the list
row_number = len(existing_tests) + 1
new_test = {
"Row Number": row_number,
"Department": department,
"Test Name": test_name
}
existing_tests.append(new_test)
return existing_tests
# Gradio interface for Consultation Tab with side-by-side layout
def gradio_interface():
with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
patient_id = gr.Number(label="Patient ID")
patient_name = gr.Textbox(label="Patient Name", interactive=False)
gr.Markdown("### Vitals")
with gr.Row():
height = gr.Number(label="Height (cms)")
weight = gr.Number(label="Weight (kgs)")
diastolic_bp = gr.Number(label="Diastolic BP")
systolic_bp = gr.Number(label="Systolic BP")
temperature = gr.Number(label="Temperature (°F)") # Updated to Fahrenheit
chief_complaints = gr.Textbox(label="Chief Complaints", lines=5)
gr.Markdown("### Lab Investigations")
with gr.Row():
department = gr.Dropdown(label="Department", choices=departments)
test_name = gr.Dropdown(label="Test Name", choices=test_names)
add_lab_test_button = gr.Button("Add Investigations")
lab_investigations = gr.Dataframe(headers=["Row Number", "Department", "Test Name"], row_count=10, col_count=3)
existing_tests = []
def update_lab_investigations(department, test_name, existing_tests):
updated_tests = add_lab_test(test_name, department, existing_tests)
return pd.DataFrame(updated_tests)
add_lab_test_button.click(
fn=update_lab_investigations,
inputs=[department, test_name, gr.State(existing_tests)],
outputs=lab_investigations
)
revisit_date = gr.Textbox(label="Revisit Date (YYYY-MM-DD)", placeholder="YYYY-MM-DD")
revisit_time = gr.Textbox(label="Revisit Time (HH:MM)", placeholder="HH:MM")
# Prescription section
with gr.Column():
gr.Markdown("### Prescription")
drug_name = gr.Dropdown(label="Drug Name", choices=medicine_names)
frequency = gr.Dropdown(label="Frequency", choices=frequencies)
quantity = gr.Number(label="Quantity")
add_prescription_button = gr.Button("Add Medicine")
prescription_list = gr.Dataframe(headers=["Row Number", "Drug Name", "Frequency", "Quantity"], row_count=10, col_count=4)
existing_prescriptions = []
def update_prescription_list(drug_name, frequency, quantity, existing_prescriptions):
updated_prescriptions = add_drug(drug_name, frequency, quantity, existing_prescriptions)
return pd.DataFrame(updated_prescriptions)
add_prescription_button.click(
fn=update_prescription_list,
inputs=[drug_name, frequency, quantity, gr.State(existing_prescriptions)],
outputs=prescription_list
)
with gr.Column():
submit_button = gr.Button("Submit")
output_text = gr.Textbox(label="Form Submission Result", interactive=False)
submit_button.click(
fn=lambda id, h, w, dbp, sbp, temp, cc, pres, li, rd, rt: submit_consultation(
id, h, w, dbp, sbp, temp, cc, pres, li, rd, rt
),
inputs=[patient_id, height, weight, diastolic_bp, systolic_bp, temperature, chief_complaints, prescription_list, lab_investigations, revisit_date, revisit_time],
outputs=output_text
)
# Auto-populate patient name when Patient ID is entered
patient_id.change(
fn=autofill_patient_name,
inputs=patient_id,
outputs=patient_name
)
return demo
# Run the Gradio app
if __name__ == "__main__":
interface = gradio_interface()
interface.launch()