Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,174 +4,149 @@ import datetime as dt
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import io
|
6 |
|
7 |
-
# Function to load data
|
8 |
@st.cache_data
|
9 |
-
def load_data(
|
10 |
-
|
11 |
-
df = pd.read_csv(file)
|
12 |
df['Date of Birth'] = pd.to_datetime(df['Date of Birth'], format='%Y-%m-%d', errors='coerce')
|
13 |
df['Visit Date'] = pd.to_datetime(df['Visit Date'], errors='coerce')
|
14 |
df['Age'] = df['Date of Birth'].apply(lambda dob: (dt.datetime.now() - dob).days // 365)
|
15 |
return df
|
16 |
|
17 |
-
#
|
18 |
-
|
19 |
-
if uploaded_file:
|
20 |
-
df = load_data(uploaded_file)
|
21 |
-
buffer = io.StringIO()
|
22 |
-
df.to_csv(buffer, index=False)
|
23 |
-
buffer.seek(0)
|
24 |
-
bytes_data = io.BytesIO(buffer.getvalue().encode('utf-8'))
|
25 |
|
26 |
-
|
27 |
-
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
if not patient_data.empty:
|
35 |
-
st.header("General Information")
|
36 |
-
st.write(f"**Patient ID:** {patient_id}")
|
37 |
-
st.write(f"**Name:** {patient_data.iloc[0]['Patient Name']}")
|
38 |
-
st.write(f"**Date of Birth:** {patient_data.iloc[0]['Date of Birth'].strftime('%Y-%m-%d')}")
|
39 |
-
st.write(f"**Age:** {patient_data.iloc[0]['Age']}")
|
40 |
-
st.write(f"**Gender:** {patient_data.iloc[0]['Gender']}")
|
41 |
-
st.write(f"**Medical History:** {patient_data.iloc[0]['Medical History']}")
|
42 |
-
st.write(f"**Allergies:** {patient_data.iloc[0]['Allergies']}")
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
st.pyplot(fig)
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
other_notes = st.text_area("Other Notes")
|
70 |
-
submitted = st.form_submit_button("Add Entry")
|
71 |
-
if submitted:
|
72 |
-
new_entry = {
|
73 |
-
"Patient ID": patient_id,
|
74 |
-
"Patient Name": patient_data.iloc[0]['Patient Name'],
|
75 |
-
"Date of Birth": patient_data.iloc[0]['Date of Birth'],
|
76 |
-
"Age": patient_data.iloc[0]['Age'],
|
77 |
-
"Gender": patient_data.iloc[0]['Gender'],
|
78 |
-
"Medical History": patient_data.iloc[0]['Medical History'],
|
79 |
-
"Allergies": patient_data.iloc[0]['Allergies'],
|
80 |
-
"Visit Date": new_visit_date,
|
81 |
-
"Complaint": complaint,
|
82 |
-
"Physical Examination": physical_exam,
|
83 |
-
"Systolic BP": systolic_bp,
|
84 |
-
"Diastolic BP": diastolic_bp,
|
85 |
-
"Temperature": temperature,
|
86 |
-
"Glucose": glucose,
|
87 |
-
"Cholesterol": cholesterol,
|
88 |
-
"Hemoglobin": hemoglobin,
|
89 |
-
"Other Notes": other_notes,
|
90 |
-
}
|
91 |
-
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
data=bytes_data,
|
108 |
-
file_name="updated_patient_data.csv",
|
109 |
-
mime="text/csv"
|
110 |
-
)
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
else:
|
127 |
-
st.write("No visit data available for the selected date.")
|
128 |
else:
|
129 |
-
st.write("No
|
|
|
|
|
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 |
-
# Save the updated data to CSV
|
165 |
-
buffer = io.StringIO()
|
166 |
-
df.to_csv(buffer, index=False)
|
167 |
-
buffer.seek(0)
|
168 |
-
bytes_data = io.BytesIO(buffer.getvalue().encode('utf-8'))
|
169 |
-
st.success("New patient account created successfully!")
|
170 |
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
data=bytes_data,
|
175 |
-
file_name="updated_patient_data.csv",
|
176 |
-
mime="text/csv"
|
177 |
-
)
|
|
|
4 |
import matplotlib.pyplot as plt
|
5 |
import io
|
6 |
|
7 |
+
# Function to load data from the local CSV file
|
8 |
@st.cache_data
|
9 |
+
def load_data():
|
10 |
+
df = pd.read_csv('patient_data.csv')
|
|
|
11 |
df['Date of Birth'] = pd.to_datetime(df['Date of Birth'], format='%Y-%m-%d', errors='coerce')
|
12 |
df['Visit Date'] = pd.to_datetime(df['Visit Date'], errors='coerce')
|
13 |
df['Age'] = df['Date of Birth'].apply(lambda dob: (dt.datetime.now() - dob).days // 365)
|
14 |
return df
|
15 |
|
16 |
+
# Load data from the CSV file
|
17 |
+
df = load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Function to save data to the local CSV file
|
20 |
+
def save_data(df):
|
21 |
+
df.to_csv('patient_data.csv', index=False)
|
22 |
|
23 |
+
# Sidebar: Select or create patient
|
24 |
+
action = st.sidebar.selectbox("Action", ["Select Patient", "Create New Patient"])
|
25 |
|
26 |
+
if action == "Select Patient":
|
27 |
+
patient_id = st.sidebar.selectbox("Patient ID", df['Patient ID'].unique())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
# Display patient data
|
30 |
+
patient_data = df[df['Patient ID'] == patient_id]
|
31 |
+
if not patient_data.empty:
|
32 |
+
st.header("General Information")
|
33 |
+
st.write(f"**Patient ID:** {patient_id}")
|
34 |
+
st.write(f"**Name:** {patient_data.iloc[0]['Patient Name']}")
|
35 |
+
st.write(f"**Date of Birth:** {patient_data.iloc[0]['Date of Birth'].strftime('%Y-%m-%d')}")
|
36 |
+
st.write(f"**Age:** {patient_data.iloc[0]['Age']}")
|
37 |
+
st.write(f"**Gender:** {patient_data.iloc[0]['Gender']}")
|
38 |
+
st.write(f"**Medical History:** {patient_data.iloc[0]['Medical History']}")
|
39 |
+
st.write(f"**Allergies:** {patient_data.iloc[0]['Allergies']}")
|
|
|
40 |
|
41 |
+
# Graphs of medical data
|
42 |
+
st.header("Medical Data Over Time")
|
43 |
+
fig, ax = plt.subplots(2, 2, figsize=(12, 8))
|
44 |
+
ax[0, 0].plot(patient_data['Visit Date'], patient_data['Systolic BP'])
|
45 |
+
ax[0, 0].set_title("Systolic Blood Pressure")
|
46 |
+
ax[0, 1].plot(patient_data['Visit Date'], patient_data['Glucose'])
|
47 |
+
ax[0, 1].set_title("Glucose")
|
48 |
+
ax[1, 0].plot(patient_data['Visit Date'], patient_data['Cholesterol'])
|
49 |
+
ax[1, 0].set_title("Cholesterol")
|
50 |
+
ax[1, 1].plot(patient_data['Visit Date'], patient_data['Hemoglobin'])
|
51 |
+
ax[1, 1].set_title("Hemoglobin")
|
52 |
+
st.pyplot(fig)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
+
# Current visit input
|
55 |
+
st.header("Current Visit")
|
56 |
+
with st.form("current_visit_form"):
|
57 |
+
new_visit_date = st.date_input("Visit Date", dt.date.today())
|
58 |
+
complaint = st.text_area("Complaint")
|
59 |
+
physical_exam = st.text_area("Physical Examination")
|
60 |
+
systolic_bp = st.number_input("Systolic Blood Pressure", min_value=0)
|
61 |
+
diastolic_bp = st.number_input("Diastolic Blood Pressure", min_value=0)
|
62 |
+
temperature = st.number_input("Temperature", min_value=0.0, format="%.1f")
|
63 |
+
glucose = st.number_input("Glucose", min_value=0)
|
64 |
+
cholesterol = st.number_input("Cholesterol", min_value=0)
|
65 |
+
hemoglobin = st.number_input("Hemoglobin", min_value=0)
|
66 |
+
other_notes = st.text_area("Other Notes")
|
67 |
+
submitted = st.form_submit_button("Add Entry")
|
68 |
+
if submitted:
|
69 |
+
new_entry = {
|
70 |
+
"Patient ID": patient_id,
|
71 |
+
"Patient Name": patient_data.iloc[0]['Patient Name'],
|
72 |
+
"Date of Birth": patient_data.iloc[0]['Date of Birth'],
|
73 |
+
"Age": patient_data.iloc[0]['Age'],
|
74 |
+
"Gender": patient_data.iloc[0]['Gender'],
|
75 |
+
"Medical History": patient_data.iloc[0]['Medical History'],
|
76 |
+
"Allergies": patient_data.iloc[0]['Allergies'],
|
77 |
+
"Visit Date": new_visit_date,
|
78 |
+
"Complaint": complaint,
|
79 |
+
"Physical Examination": physical_exam,
|
80 |
+
"Systolic BP": systolic_bp,
|
81 |
+
"Diastolic BP": diastolic_bp,
|
82 |
+
"Temperature": temperature,
|
83 |
+
"Glucose": glucose,
|
84 |
+
"Cholesterol": cholesterol,
|
85 |
+
"Hemoglobin": hemoglobin,
|
86 |
+
"Other Notes": other_notes,
|
87 |
+
}
|
88 |
+
df = pd.concat([df, pd.DataFrame([new_entry])], ignore_index=True)
|
89 |
|
90 |
+
# Save the updated data to CSV
|
91 |
+
save_data(df)
|
92 |
+
st.success("New visit entry added successfully!")
|
93 |
|
94 |
+
# Reload data
|
95 |
+
df = load_data()
|
96 |
+
patient_data = df[df['Patient ID'] == patient_id]
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
# Dropdown menu of previous visits
|
99 |
+
st.header("Previous Visits")
|
100 |
+
visit_date = st.selectbox("Select Visit Date", patient_data['Visit Date'].dt.strftime('%Y-%m-%d').unique())
|
101 |
+
visit_data = patient_data[patient_data['Visit Date'] == pd.to_datetime(visit_date)]
|
102 |
+
if not visit_data.empty:
|
103 |
+
st.write(f"**Complaint:** {visit_data.iloc[0]['Complaint']}")
|
104 |
+
st.write(f"**Physical Examination:** {visit_data.iloc[0]['Physical Examination']}")
|
105 |
+
st.write(f"**Systolic BP:** {visit_data.iloc[0]['Systolic BP']}")
|
106 |
+
st.write(f"**Diastolic BP:** {visit_data.iloc[0]['Diastolic BP']}")
|
107 |
+
st.write(f"**Temperature:** {visit_data.iloc[0]['Temperature']}")
|
108 |
+
st.write(f"**Glucose:** {visit_data.iloc[0]['Glucose']}")
|
109 |
+
st.write(f"**Cholesterol:** {visit_data.iloc[0]['Cholesterol']}")
|
110 |
+
st.write(f"**Hemoglobin:** {visit_data.iloc[0]['Hemoglobin']}")
|
111 |
+
st.write(f"**Other Notes:** {visit_data.iloc[0]['Other Notes']}")
|
|
|
|
|
112 |
else:
|
113 |
+
st.write("No visit data available for the selected date.")
|
114 |
+
else:
|
115 |
+
st.write("No patient data available.")
|
116 |
|
117 |
+
elif action == "Create New Patient":
|
118 |
+
st.header("Create New Patient Account")
|
119 |
+
with st.form("new_patient_form"):
|
120 |
+
new_patient_id = st.text_input("Patient ID")
|
121 |
+
new_patient_name = st.text_input("Patient Name")
|
122 |
+
new_dob = st.date_input("Date of Birth")
|
123 |
+
new_age = (dt.datetime.now() - pd.to_datetime(new_dob)).days // 365
|
124 |
+
new_gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
125 |
+
new_med_history = st.text_area("Medical History")
|
126 |
+
new_allergies = st.text_area("Allergies")
|
127 |
+
new_submitted = st.form_submit_button("Add New Patient")
|
128 |
+
if new_submitted:
|
129 |
+
new_patient = {
|
130 |
+
"Patient ID": new_patient_id,
|
131 |
+
"Patient Name": new_patient_name,
|
132 |
+
"Date of Birth": new_dob,
|
133 |
+
"Age": new_age,
|
134 |
+
"Gender": new_gender,
|
135 |
+
"Medical History": new_med_history,
|
136 |
+
"Allergies": new_allergies,
|
137 |
+
"Visit Date": None,
|
138 |
+
"Complaint": None,
|
139 |
+
"Physical Examination": None,
|
140 |
+
"Systolic BP": None,
|
141 |
+
"Diastolic BP": None,
|
142 |
+
"Temperature": None,
|
143 |
+
"Glucose": None,
|
144 |
+
"Cholesterol": None,
|
145 |
+
"Hemoglobin": None,
|
146 |
+
"Other Notes": None,
|
147 |
+
}
|
148 |
+
df = pd.concat([df, pd.DataFrame([new_patient])], ignore_index=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
+
# Save the updated data to CSV
|
151 |
+
save_data(df)
|
152 |
+
st.success("New patient account created successfully!")
|
|
|
|
|
|
|
|