Spaces:
Build error
Build error
Update app.py
Browse files123345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114256789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135import streamlit as stimport pandas as pdimport plotly.express as pximport datetime as dtimport matplotlib.pyplot as pltfrom datetime import datetime
# Function to load patient datadef load_data(): return pd.read_csv('patient_data.csv')
# Function to save patient datadef save_data(df): df.to_csv('patient_data_extended.csv', index=False)
# Load the existing datadf = load_data()
# Sidebar for navigationst.sidebar.title("Navigation")options = ["View Patient Data", "Add New Patient", "Add New Visit"]choice = st.sidebar.selectbox("Choose an option", options)
if choice == "View Patient Data": # Sidebar for Patient Selection st.sidebar.header('Select Patient') patient_id = st.sidebar.selectbox('Patient ID', df['patient_id'].unique())
# Filter Data for Selected Patient patient_data = df[df['patient_id'] == patient_id]
# Display Patient Profile st.header('Patient Profile') st.write(f"Name: {patient_data['name'].values[0]}") st.write(f"Age: {patient_data['age'].values[0]}") st.write(f"Gender: {patient_data['gender'].values[0]}") st.write(f"Medical History: {patient_data['medical_history'].values[0]}")
# Visualization of Vital Signs st.header('Vital Signs Over Time')
# Line Chart for Heart Rate fig = px.line(patient_data, x='date', y='heart_rate', title='Heart Rate Over Time') st.plotly_chart(fig)
# Line Chart for Blood Pressure fig = px.line(patient_data, x='date', y='blood_pressure', title='Blood Pressure Over Time') st.plotly_chart(fig)
# Line Chart for Blood Glucose fig = px.line(patient_data, x='date', y='blood_glucose', title='Blood Glucose Over Time') st.plotly_chart(fig)
# Dropdown for selecting specific visit details st.header('Previous Visit Details') selected_date = st.selectbox('Select Visit Date', patient_data['date'].unique()) selected_visit = patient_data[patient_data['date'] == selected_date]
st.write(f"**Visit Date:** {selected_date}") st.write(f"Heart Rate: {selected_visit['heart_rate'].values[0]}") st.write(f"Blood Pressure: {selected_visit['blood_pressure'].values[0]}") st.write(f"Blood Glucose: {selected_visit['blood_glucose'].values[0]}")
# Alerts and Notifications st.header('Alerts') if selected_visit['heart_rate'].values[0] > 100: st.error('High heart rate detected!') if selected_visit['blood_pressure'].values[0] > 140: st.error('High blood pressure detected!')
# Download Button for Patient Data st.download_button( label="Download Patient Data as CSV", data=patient_data.to_csv().encode('utf-8'), file_name=f'patient_{patient_id}_data.csv', mime='text/csv', )
elif choice == "Add New Patient": st.header("Add New Patient")
# Input fields for new patient data new_patient_id = st.number_input("Patient ID", min_value=0, step=1) new_name = st.text_input("Name") new_age = st.number_input("Age", min_value=0, step=1) new_gender = st.selectbox("Gender", ["Male", "Female"]) new_medical_history = st.text_area("Medical History")
if st.button("Add Patient"): new_patient_data = { 'patient_id': new_patient_id, 'name': new_name, 'age': new_age, 'gender': new_gender, 'medical_history': new_medical_history, 'date': None, 'heart_rate': None, 'blood_pressure': None, 'blood_glucose': None } df = pd.concat([df, pd.DataFrame([new_patient_data])], ignore_index=True) save_data(df) st.success("New patient added successfully!")
elif choice == "Add New Visit": st.header("Add New Visit")
# Input fields for adding a new visit patient_id = st.number_input("Patient ID", min_value=0, step=1) visit_date = st.date_input("Date of Visit", value=datetime.today()) medical_complaints = st.text_area("Medical Complaints") symptoms = st.text_area("Symptoms") physical_examination = st.text_area("Physical Examination") diagnosis = st.text_area("Diagnosis") heart_rate = st.number_input("Heart Rate", min_value=0) blood_pressure = st.number_input("Blood Pressure", min_value=0) temperature = st.number_input("Temperature", min_value=0) glucose = st.number_input("Blood Glucose", min_value=0) extra_notes = st.text_area("Extra Notes") treatment = st.text_area("Treatment")
if st.button("Add Visit"): new_visit_data = { 'patient_id': patient_id, 'name': df[df['patient_id'] == patient_id]['name'].values[0], 'age': df[df['patient_id'] == patient_id]['age'].values[0], 'gender': df[df['patient_id'] == patient_id]['gender'].values[0], 'medical_history': df[df['patient_id'] == patient_id]['medical_history'].values[0], 'date': visit_date, 'heart_rate': heart_rate, 'blood_pressure': blood_pressure, 'blood_glucose': glucose, 'temperature': temperature, 'medical_complaints': medical_complaints, 'symptoms': symptoms, 'physical_examination': physical_examination, 'diagnosis': diagnosis, 'extra_notes': extra_notes, 'treatment': treatment } df = pd.concat([df, pd.DataFrame([new_visit_data])], ignore_index=True) save_data(df) st.success("New visit added successfully!")
# Function to load data@st.cachedef load_data(file): df = pd.read_csv(file) df['Date of Birth'] = pd.to_datetime(df['Date of Birth']) df['Age'] = (pd.Timestamp.now() - df['Date of Birth']).astype('<m8[Y]') df['Visit Date'] = pd.to_datetime(df['Visit Date']) return df
# Sidebar: Upload CSV fileuploaded_file = st.sidebar.file_uploader("Upload Patient Data CSV", type="csv")if uploaded_file: df = load_data(uploaded_file)
# Sidebar: Select or create patient action = st.sidebar.selectbox("Action", ["Select Patient", "Create New Patient"])
if action == "Select Patient": patient_id = st.sidebar.selectbox("Patient ID", df['Patient ID'].unique())
# Display patient data patient_data = df[df['Patient ID'] == patient_id] st.header("General Information") st.write(f"**Patient ID:** {patient_id}") st.write(f"**Name:** {patient_data.iloc[0]['Patient Name']}") st.write(f"**Date of Birth:** {patient_data.iloc[0]['Date of Birth'].strftime('%Y-%m-%d')}") st.write(f"**Age:** {patient_data.iloc[0]['Age']}") st.write(f"**Gender:** {patient_data.iloc[0]['Gender']}") st.write(f"**Medical History:** {patient_data.iloc[0]['Medical History']}") st.write(f"**Allergies:** {patient_data.iloc[0]['Allergies']}")
# Graphs of medical data st.header("Medical Data Over Time") fig, ax = plt.subplots(2, 2, figsize=(12, 8)) ax[0, 0].plot(patient_data['Visit Date'], patient_data['Systolic BP']) ax[0, 0].set_title("Systolic Blood Pressure") ax[0, 1].plot(patient_data['Visit Date'], patient_data['Glucose']) ax[0, 1].set_title("Glucose") ax[1, 0].plot(patient_data['Visit Date'], patient_data['Cholesterol']) ax[1, 0].set_title("Cholesterol") ax[1, 1].plot(patient_data['Visit Date'], patient_data['Hemoglobin']) ax[1, 1].set_title("Hemoglobin") st.pyplot(fig)
# Dropdown menu of previous visits st.header("Previous Visits") visit_date = st.selectbox("Select Visit Date", patient_data['Visit Date'].dt.strftime('%Y-%m-%d').unique()) visit_data = patient_data[patient_data['Visit Date'] == pd.to_datetime(visit_date)] st.write(f"**Complaint:** {visit_data.iloc[0]['Complaint']}") st.write(f"**Physical Examination:** {visit_data.iloc[0]['Physical Examination']}") st.write(f"**Systolic BP:** {visit_data.iloc[0]['Systolic BP']}") st.write(f"**Diastolic BP:** {visit_data.iloc[0]['Diastolic BP']}") st.write(f"**Temperature:** {visit_data.iloc[0]['Temperature']}") st.write(f"**Glucose:** {visit_data.iloc[0]['Glucose']}") st.write(f"**Cholesterol:** {visit_data.iloc[0]['Cholesterol']}") st.write(f"**Hemoglobin:** {visit_data.iloc[0]['Hemoglobin']}") st.write(f"**Other Notes:** {visit_data.iloc[0]['Other Notes']}")
# Current visit input st.header("Current Visit") with st.form("current_visit_form"): new_visit_date = st.date_input("Visit Date", dt.date.today()) complaint = st.text_area("Complaint") physical_exam = st.text_area("Physical Examination") systolic_bp = st.number_input("Systolic Blood Pressure", min_value=0) diastolic_bp = st.number_input("Diastolic Blood Pressure", min_value=0) temperature = st.number_input("Temperature", min_value=0.0, format="%.1f") glucose = st.number_input("Glucose", min_value=0) cholesterol = st.number_input("Cholesterol", min_value=0) hemoglobin = st.number_input("Hemoglobin", min_value=0) other_notes = st.text_area("Other Notes") submitted = st.form_submit_button("Add Entry") if submitted: new_entry = { "Patient ID": patient_id, "Patient Name": patient_data.iloc[0]['Patient Name'], "Date of Birth": patient_data.iloc[0]['Date of Birth'], "Age": patient_data.iloc[0]['Age'], "Gender": patient_data.iloc[0]['Gender'], "Medical History": patient_data.iloc[0]['Medical History'], "Allergies": patient_data.iloc[0]['Allergies'], "Visit Date": new_visit_date, "Complaint": complaint, "Physical Examination": physical_exam, "Systolic BP": systolic_bp, "Diastolic BP": diastolic_bp, "Temperature": temperature, "Glucose": glucose, "Cholesterol": cholesterol, "Hemoglobin": hemoglobin, "Other Notes": other_notes, } df = df.append(new_entry, ignore_index=True) df.to_csv(uploaded_file, index=False) st.success("New visit entry added successfully!")
elif action == "Create New Patient": st.header("Create New Patient Account") with st.form("new_patient_form"): new_patient_id = st.text_input("Patient ID") new_patient_name = st.text_input("Patient Name") new_dob = st.date_input("Date of Birth") new_age = (pd.Timestamp.now() - pd.to_datetime(new_dob)).days // 365 new_gender = st.selectbox("Gender", ["Male", "Female", "Other"]) new_med_history = st.text_area("Medical History") new_allergies = st.text_area("Allergies") new_submitted = st.form_submit_button("Add New Patient") if new_submitted: new_patient = { "Patient ID": new_patient_id, "Patient Name": new_patient_name, "Date of Birth": new_dob, "Age": new_age, "Gender": new_gender, "Medical History": new_med_history, "Allergies": new_allergies, "Visit Date": None, "Complaint": None, "Physical Examination": None, "Systolic BP": None, "Diastolic BP": None, "Temperature": None, "Glucose": None, "Cholesterol": None, "Hemoglobin": None, "Other Notes": None, } df = df.append(new_patient, ignore_index=True) df.to_csv(uploaded_file, index=False) st.success("New patient account created successfully!")app.py+131-139