Spaces:
Sleeping
Sleeping
File size: 8,105 Bytes
40cbaf1 9188f50 0a5028c 6052b4c 415a9d1 9188f50 b6d1d8d 9188f50 6052b4c 9188f50 c58b33a f631d2a 9188f50 e318235 9188f50 3dd320c 6052b4c 65483d1 9188f50 e318235 e3f7da8 65483d1 e3f7da8 f8e6406 9188f50 f631d2a 9188f50 3dd320c 6052b4c 65483d1 9188f50 e3f7da8 65483d1 e3f7da8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
import streamlit as st
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
import io
# Function to load data
@st.cache_data
def load_data(file):
file.seek(0) # Move to the start of the file
df = pd.read_csv(file)
df['Date of Birth'] = pd.to_datetime(df['Date of Birth'], format='%Y-%m-%d', errors='coerce')
df['Visit Date'] = pd.to_datetime(df['Visit Date'], format='%Y-%m-%d', errors='coerce')
df['Age'] = df['Date of Birth'].apply(lambda dob: (dt.datetime.now() - dob).days // 365)
return df
# Sidebar: Upload CSV file
uploaded_file = st.sidebar.file_uploader("Upload Patient Data CSV", type="csv")
if uploaded_file:
df = load_data(uploaded_file)
buffer = io.StringIO()
df.to_csv(buffer, index=False)
buffer.seek(0)
bytes_data = io.BytesIO(buffer.getvalue().encode('utf-8'))
# 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)
# 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 = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
# Save the updated data to CSV
buffer = io.StringIO()
df.to_csv(buffer, index=False)
buffer.seek(0)
bytes_data = io.BytesIO(buffer.getvalue().encode('utf-8'))
st.success("New visit entry added successfully!")
# Reload data
df = load_data(io.StringIO(buffer.getvalue()))
patient_data = df[df['Patient ID'] == patient_id]
# Download button (moved outside the form)
st.download_button(
label="Download Updated Data",
data=bytes_data,
file_name="updated_patient_data.csv",
mime="text/csv"
)
# 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']}")
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 = (dt.datetime.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 = pd.concat([df, pd.DataFrame([new_patient])], ignore_index=True)
# Save the updated data to CSV
buffer = io.StringIO()
df.to_csv(buffer, index=False)
buffer.seek(0)
bytes_data = io.BytesIO(buffer.getvalue().encode('utf-8'))
st.success("New patient account created successfully!")
# Download button (moved outside the form)
st.download_button(
label="Download Updated Data",
data=bytes_data,
file_name="updated_patient_data.csv",
mime="text/csv"
)
|