Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,135 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
-
import
|
4 |
import matplotlib.pyplot as plt
|
5 |
-
from datetime import datetime
|
6 |
-
|
7 |
-
# Function to load patient data
|
8 |
-
def load_data():
|
9 |
-
return pd.read_csv('patient_data.csv')
|
10 |
-
|
11 |
-
# Function to save patient data
|
12 |
-
def save_data(df):
|
13 |
-
df.to_csv('patient_data_extended.csv', index=False)
|
14 |
-
|
15 |
-
# Load the existing data
|
16 |
-
df = load_data()
|
17 |
-
|
18 |
-
# Sidebar for navigation
|
19 |
-
st.sidebar.title("Navigation")
|
20 |
-
options = ["View Patient Data", "Add New Patient", "Add New Visit"]
|
21 |
-
choice = st.sidebar.selectbox("Choose an option", options)
|
22 |
-
|
23 |
-
if choice == "View Patient Data":
|
24 |
-
# Sidebar for Patient Selection
|
25 |
-
st.sidebar.header('Select Patient')
|
26 |
-
patient_id = st.sidebar.selectbox('Patient ID', df['patient_id'].unique())
|
27 |
-
|
28 |
-
# Filter Data for Selected Patient
|
29 |
-
patient_data = df[df['patient_id'] == patient_id]
|
30 |
-
|
31 |
-
# Display Patient Profile
|
32 |
-
st.header('Patient Profile')
|
33 |
-
st.write(f"Name: {patient_data['name'].values[0]}")
|
34 |
-
st.write(f"Age: {patient_data['age'].values[0]}")
|
35 |
-
st.write(f"Gender: {patient_data['gender'].values[0]}")
|
36 |
-
st.write(f"Medical History: {patient_data['medical_history'].values[0]}")
|
37 |
-
|
38 |
-
# Visualization of Vital Signs
|
39 |
-
st.header('Vital Signs Over Time')
|
40 |
-
|
41 |
-
# Line Chart for Heart Rate
|
42 |
-
fig = px.line(patient_data, x='date', y='heart_rate', title='Heart Rate Over Time')
|
43 |
-
st.plotly_chart(fig)
|
44 |
-
|
45 |
-
# Line Chart for Blood Pressure
|
46 |
-
fig = px.line(patient_data, x='date', y='blood_pressure', title='Blood Pressure Over Time')
|
47 |
-
st.plotly_chart(fig)
|
48 |
-
|
49 |
-
# Line Chart for Blood Glucose
|
50 |
-
fig = px.line(patient_data, x='date', y='blood_glucose', title='Blood Glucose Over Time')
|
51 |
-
st.plotly_chart(fig)
|
52 |
-
|
53 |
-
# Dropdown for selecting specific visit details
|
54 |
-
st.header('Previous Visit Details')
|
55 |
-
selected_date = st.selectbox('Select Visit Date', patient_data['date'].unique())
|
56 |
-
selected_visit = patient_data[patient_data['date'] == selected_date]
|
57 |
-
|
58 |
-
st.write(f"**Visit Date:** {selected_date}")
|
59 |
-
st.write(f"Heart Rate: {selected_visit['heart_rate'].values[0]}")
|
60 |
-
st.write(f"Blood Pressure: {selected_visit['blood_pressure'].values[0]}")
|
61 |
-
st.write(f"Blood Glucose: {selected_visit['blood_glucose'].values[0]}")
|
62 |
-
|
63 |
-
# Alerts and Notifications
|
64 |
-
st.header('Alerts')
|
65 |
-
if selected_visit['heart_rate'].values[0] > 100:
|
66 |
-
st.error('High heart rate detected!')
|
67 |
-
if selected_visit['blood_pressure'].values[0] > 140:
|
68 |
-
st.error('High blood pressure detected!')
|
69 |
-
|
70 |
-
# Download Button for Patient Data
|
71 |
-
st.download_button(
|
72 |
-
label="Download Patient Data as CSV",
|
73 |
-
data=patient_data.to_csv().encode('utf-8'),
|
74 |
-
file_name=f'patient_{patient_id}_data.csv',
|
75 |
-
mime='text/csv',
|
76 |
-
)
|
77 |
-
|
78 |
-
elif choice == "Add New Patient":
|
79 |
-
st.header("Add New Patient")
|
80 |
-
|
81 |
-
# Input fields for new patient data
|
82 |
-
new_patient_id = st.number_input("Patient ID", min_value=0, step=1)
|
83 |
-
new_name = st.text_input("Name")
|
84 |
-
new_age = st.number_input("Age", min_value=0, step=1)
|
85 |
-
new_gender = st.selectbox("Gender", ["Male", "Female"])
|
86 |
-
new_medical_history = st.text_area("Medical History")
|
87 |
-
|
88 |
-
if st.button("Add Patient"):
|
89 |
-
new_patient_data = {
|
90 |
-
'patient_id': new_patient_id,
|
91 |
-
'name': new_name,
|
92 |
-
'age': new_age,
|
93 |
-
'gender': new_gender,
|
94 |
-
'medical_history': new_medical_history,
|
95 |
-
'date': None,
|
96 |
-
'heart_rate': None,
|
97 |
-
'blood_pressure': None,
|
98 |
-
'blood_glucose': None
|
99 |
-
}
|
100 |
-
df = pd.concat([df, pd.DataFrame([new_patient_data])], ignore_index=True)
|
101 |
-
save_data(df)
|
102 |
-
st.success("New patient added successfully!")
|
103 |
-
|
104 |
-
elif choice == "Add New Visit":
|
105 |
-
st.header("Add New Visit")
|
106 |
-
|
107 |
-
# Input fields for adding a new visit
|
108 |
-
patient_id = st.number_input("Patient ID", min_value=0, step=1)
|
109 |
-
visit_date = st.date_input("Date of Visit", value=datetime.today())
|
110 |
-
medical_complaints = st.text_area("Medical Complaints")
|
111 |
-
symptoms = st.text_area("Symptoms")
|
112 |
-
physical_examination = st.text_area("Physical Examination")
|
113 |
-
diagnosis = st.text_area("Diagnosis")
|
114 |
-
heart_rate = st.number_input("Heart Rate", min_value=0)
|
115 |
-
blood_pressure = st.number_input("Blood Pressure", min_value=0)
|
116 |
-
temperature = st.number_input("Temperature", min_value=0)
|
117 |
-
glucose = st.number_input("Blood Glucose", min_value=0)
|
118 |
-
extra_notes = st.text_area("Extra Notes")
|
119 |
-
treatment = st.text_area("Treatment")
|
120 |
-
|
121 |
-
if st.button("Add Visit"):
|
122 |
-
new_visit_data = {
|
123 |
-
'patient_id': patient_id,
|
124 |
-
'name': df[df['patient_id'] == patient_id]['name'].values[0],
|
125 |
-
'age': df[df['patient_id'] == patient_id]['age'].values[0],
|
126 |
-
'gender': df[df['patient_id'] == patient_id]['gender'].values[0],
|
127 |
-
'medical_history': df[df['patient_id'] == patient_id]['medical_history'].values[0],
|
128 |
-
'date': visit_date,
|
129 |
-
'heart_rate': heart_rate,
|
130 |
-
'blood_pressure': blood_pressure,
|
131 |
-
'blood_glucose': glucose,
|
132 |
-
'temperature': temperature,
|
133 |
-
'medical_complaints': medical_complaints,
|
134 |
-
'symptoms': symptoms,
|
135 |
-
'physical_examination': physical_examination,
|
136 |
-
'diagnosis': diagnosis,
|
137 |
-
'extra_notes': extra_notes,
|
138 |
-
'treatment': treatment
|
139 |
-
}
|
140 |
-
df = pd.concat([df, pd.DataFrame([new_visit_data])], ignore_index=True)
|
141 |
-
save_data(df)
|
142 |
-
st.success("New visit added successfully!")
|
143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import datetime as dt
|
4 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Function to load data
|
7 |
+
@st.cache
|
8 |
+
def load_data(file):
|
9 |
+
df = pd.read_csv(file)
|
10 |
+
df['Date of Birth'] = pd.to_datetime(df['Date of Birth'])
|
11 |
+
df['Age'] = (pd.Timestamp.now() - df['Date of Birth']).astype('<m8[Y]')
|
12 |
+
df['Visit Date'] = pd.to_datetime(df['Visit Date'])
|
13 |
+
return df
|
14 |
+
|
15 |
+
# Sidebar: Upload CSV file
|
16 |
+
uploaded_file = st.sidebar.file_uploader("Upload Patient Data CSV", type="csv")
|
17 |
+
if uploaded_file:
|
18 |
+
df = load_data(uploaded_file)
|
19 |
+
|
20 |
+
# Sidebar: Select or create patient
|
21 |
+
action = st.sidebar.selectbox("Action", ["Select Patient", "Create New Patient"])
|
22 |
+
|
23 |
+
if action == "Select Patient":
|
24 |
+
patient_id = st.sidebar.selectbox("Patient ID", df['Patient ID'].unique())
|
25 |
+
|
26 |
+
# Display patient data
|
27 |
+
patient_data = df[df['Patient ID'] == patient_id]
|
28 |
+
st.header("General Information")
|
29 |
+
st.write(f"**Patient ID:** {patient_id}")
|
30 |
+
st.write(f"**Name:** {patient_data.iloc[0]['Patient Name']}")
|
31 |
+
st.write(f"**Date of Birth:** {patient_data.iloc[0]['Date of Birth'].strftime('%Y-%m-%d')}")
|
32 |
+
st.write(f"**Age:** {patient_data.iloc[0]['Age']}")
|
33 |
+
st.write(f"**Gender:** {patient_data.iloc[0]['Gender']}")
|
34 |
+
st.write(f"**Medical History:** {patient_data.iloc[0]['Medical History']}")
|
35 |
+
st.write(f"**Allergies:** {patient_data.iloc[0]['Allergies']}")
|
36 |
+
|
37 |
+
# Graphs of medical data
|
38 |
+
st.header("Medical Data Over Time")
|
39 |
+
fig, ax = plt.subplots(2, 2, figsize=(12, 8))
|
40 |
+
ax[0, 0].plot(patient_data['Visit Date'], patient_data['Systolic BP'])
|
41 |
+
ax[0, 0].set_title("Systolic Blood Pressure")
|
42 |
+
ax[0, 1].plot(patient_data['Visit Date'], patient_data['Glucose'])
|
43 |
+
ax[0, 1].set_title("Glucose")
|
44 |
+
ax[1, 0].plot(patient_data['Visit Date'], patient_data['Cholesterol'])
|
45 |
+
ax[1, 0].set_title("Cholesterol")
|
46 |
+
ax[1, 1].plot(patient_data['Visit Date'], patient_data['Hemoglobin'])
|
47 |
+
ax[1, 1].set_title("Hemoglobin")
|
48 |
+
st.pyplot(fig)
|
49 |
+
|
50 |
+
# Dropdown menu of previous visits
|
51 |
+
st.header("Previous Visits")
|
52 |
+
visit_date = st.selectbox("Select Visit Date", patient_data['Visit Date'].dt.strftime('%Y-%m-%d').unique())
|
53 |
+
visit_data = patient_data[patient_data['Visit Date'] == pd.to_datetime(visit_date)]
|
54 |
+
st.write(f"**Complaint:** {visit_data.iloc[0]['Complaint']}")
|
55 |
+
st.write(f"**Physical Examination:** {visit_data.iloc[0]['Physical Examination']}")
|
56 |
+
st.write(f"**Systolic BP:** {visit_data.iloc[0]['Systolic BP']}")
|
57 |
+
st.write(f"**Diastolic BP:** {visit_data.iloc[0]['Diastolic BP']}")
|
58 |
+
st.write(f"**Temperature:** {visit_data.iloc[0]['Temperature']}")
|
59 |
+
st.write(f"**Glucose:** {visit_data.iloc[0]['Glucose']}")
|
60 |
+
st.write(f"**Cholesterol:** {visit_data.iloc[0]['Cholesterol']}")
|
61 |
+
st.write(f"**Hemoglobin:** {visit_data.iloc[0]['Hemoglobin']}")
|
62 |
+
st.write(f"**Other Notes:** {visit_data.iloc[0]['Other Notes']}")
|
63 |
+
|
64 |
+
# Current visit input
|
65 |
+
st.header("Current Visit")
|
66 |
+
with st.form("current_visit_form"):
|
67 |
+
new_visit_date = st.date_input("Visit Date", dt.date.today())
|
68 |
+
complaint = st.text_area("Complaint")
|
69 |
+
physical_exam = st.text_area("Physical Examination")
|
70 |
+
systolic_bp = st.number_input("Systolic Blood Pressure", min_value=0)
|
71 |
+
diastolic_bp = st.number_input("Diastolic Blood Pressure", min_value=0)
|
72 |
+
temperature = st.number_input("Temperature", min_value=0.0, format="%.1f")
|
73 |
+
glucose = st.number_input("Glucose", min_value=0)
|
74 |
+
cholesterol = st.number_input("Cholesterol", min_value=0)
|
75 |
+
hemoglobin = st.number_input("Hemoglobin", min_value=0)
|
76 |
+
other_notes = st.text_area("Other Notes")
|
77 |
+
submitted = st.form_submit_button("Add Entry")
|
78 |
+
if submitted:
|
79 |
+
new_entry = {
|
80 |
+
"Patient ID": patient_id,
|
81 |
+
"Patient Name": patient_data.iloc[0]['Patient Name'],
|
82 |
+
"Date of Birth": patient_data.iloc[0]['Date of Birth'],
|
83 |
+
"Age": patient_data.iloc[0]['Age'],
|
84 |
+
"Gender": patient_data.iloc[0]['Gender'],
|
85 |
+
"Medical History": patient_data.iloc[0]['Medical History'],
|
86 |
+
"Allergies": patient_data.iloc[0]['Allergies'],
|
87 |
+
"Visit Date": new_visit_date,
|
88 |
+
"Complaint": complaint,
|
89 |
+
"Physical Examination": physical_exam,
|
90 |
+
"Systolic BP": systolic_bp,
|
91 |
+
"Diastolic BP": diastolic_bp,
|
92 |
+
"Temperature": temperature,
|
93 |
+
"Glucose": glucose,
|
94 |
+
"Cholesterol": cholesterol,
|
95 |
+
"Hemoglobin": hemoglobin,
|
96 |
+
"Other Notes": other_notes,
|
97 |
+
}
|
98 |
+
df = df.append(new_entry, ignore_index=True)
|
99 |
+
df.to_csv(uploaded_file, index=False)
|
100 |
+
st.success("New visit entry added successfully!")
|
101 |
+
|
102 |
+
elif action == "Create New Patient":
|
103 |
+
st.header("Create New Patient Account")
|
104 |
+
with st.form("new_patient_form"):
|
105 |
+
new_patient_id = st.text_input("Patient ID")
|
106 |
+
new_patient_name = st.text_input("Patient Name")
|
107 |
+
new_dob = st.date_input("Date of Birth")
|
108 |
+
new_age = (pd.Timestamp.now() - pd.to_datetime(new_dob)).days // 365
|
109 |
+
new_gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
110 |
+
new_med_history = st.text_area("Medical History")
|
111 |
+
new_allergies = st.text_area("Allergies")
|
112 |
+
new_submitted = st.form_submit_button("Add New Patient")
|
113 |
+
if new_submitted:
|
114 |
+
new_patient = {
|
115 |
+
"Patient ID": new_patient_id,
|
116 |
+
"Patient Name": new_patient_name,
|
117 |
+
"Date of Birth": new_dob,
|
118 |
+
"Age": new_age,
|
119 |
+
"Gender": new_gender,
|
120 |
+
"Medical History": new_med_history,
|
121 |
+
"Allergies": new_allergies,
|
122 |
+
"Visit Date": None,
|
123 |
+
"Complaint": None,
|
124 |
+
"Physical Examination": None,
|
125 |
+
"Systolic BP": None,
|
126 |
+
"Diastolic BP": None,
|
127 |
+
"Temperature": None,
|
128 |
+
"Glucose": None,
|
129 |
+
"Cholesterol": None,
|
130 |
+
"Hemoglobin": None,
|
131 |
+
"Other Notes": None,
|
132 |
+
}
|
133 |
+
df = df.append(new_patient, ignore_index=True)
|
134 |
+
df.to_csv(uploaded_file, index=False)
|
135 |
+
st.success("New patient account created successfully!")
|