Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,150 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
# Function to load data from
|
| 8 |
def load_data():
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
df['Visit Date'] = pd.to_datetime(df['Visit Date'], errors='coerce')
|
| 12 |
-
df['Age'] = df['Date of Birth'].apply(lambda dob: (dt.datetime.now() - dob).days // 365)
|
| 13 |
-
return df
|
| 14 |
|
| 15 |
-
# Function to save data to
|
| 16 |
def save_data(df):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
# Load
|
| 21 |
df = load_data()
|
| 22 |
|
| 23 |
-
# Sidebar
|
| 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 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
|
|
|
| 5 |
|
| 6 |
+
# Function to load patient data from JSON
|
| 7 |
def load_data():
|
| 8 |
+
with open('data.json', 'r') as f:
|
| 9 |
+
return pd.DataFrame(json.load(f))
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Function to save patient data to JSON
|
| 12 |
def save_data(df):
|
| 13 |
+
with open('data.json', 'w') as f:
|
| 14 |
+
json.dump(df.to_dict(orient='records'), f, indent=4)
|
| 15 |
|
| 16 |
+
# Load the existing data
|
| 17 |
df = load_data()
|
| 18 |
|
| 19 |
+
# Sidebar for navigation
|
| 20 |
+
st.sidebar.title("Navigation")
|
| 21 |
+
options = ["View Patient Data", "Add New Patient", "Add New Visit"]
|
| 22 |
+
choice = st.sidebar.selectbox("Choose an option", options)
|
| 23 |
+
|
| 24 |
+
if choice == "View Patient Data":
|
| 25 |
+
# Sidebar for Patient Selection
|
| 26 |
+
st.sidebar.header('Select Patient')
|
| 27 |
+
patient_id = st.sidebar.selectbox('Patient ID', df['patient_id'].unique())
|
| 28 |
+
|
| 29 |
+
# Filter Data for Selected Patient
|
| 30 |
+
patient_data = df[df['patient_id'] == patient_id]
|
| 31 |
+
|
| 32 |
+
# Display Patient Profile
|
| 33 |
+
st.header('Patient Profile')
|
| 34 |
+
st.write(f"Name: {patient_data['name'].values[0]}")
|
| 35 |
+
st.write(f"Age: {patient_data['age'].values[0]}")
|
| 36 |
+
st.write(f"Gender: {patient_data['gender'].values[0]}")
|
| 37 |
+
st.write(f"Medical History: {patient_data['medical_history'].values[0]}")
|
| 38 |
+
|
| 39 |
+
# Visualization of Vital Signs
|
| 40 |
+
st.header('Vital Signs Over Time')
|
| 41 |
+
|
| 42 |
+
# Line Chart for Heart Rate
|
| 43 |
+
fig = px.line(patient_data, x='date', y='heart_rate', title='Heart Rate Over Time')
|
| 44 |
+
st.plotly_chart(fig)
|
| 45 |
+
|
| 46 |
+
# Line Chart for Blood Pressure
|
| 47 |
+
fig = px.line(patient_data, x='date', y='blood_pressure', title='Blood Pressure Over Time')
|
| 48 |
+
st.plotly_chart(fig)
|
| 49 |
+
|
| 50 |
+
# Line Chart for Blood Glucose
|
| 51 |
+
fig = px.line(patient_data, x='date', y='blood_glucose', title='Blood Glucose Over Time')
|
| 52 |
+
st.plotly_chart(fig)
|
| 53 |
+
|
| 54 |
+
# Dropdown for selecting specific visit details
|
| 55 |
+
st.header('Previous Visit Details')
|
| 56 |
+
selected_date = st.selectbox('Select Visit Date', patient_data['date'].unique())
|
| 57 |
+
selected_visit = patient_data[patient_data['date'] == selected_date]
|
| 58 |
+
|
| 59 |
+
st.write(f"**Visit Date:** {selected_date}")
|
| 60 |
+
st.write(f"Heart Rate: {selected_visit['heart_rate'].values[0]}")
|
| 61 |
+
st.write(f"Blood Pressure: {selected_visit['blood_pressure'].values[0]}")
|
| 62 |
+
st.write(f"Blood Glucose: {selected_visit['blood_glucose'].values[0]}")
|
| 63 |
+
|
| 64 |
+
# Alerts and Notifications
|
| 65 |
+
st.header('Alerts')
|
| 66 |
+
if selected_visit['heart_rate'].values[0] > 100:
|
| 67 |
+
st.error('High heart rate detected!')
|
| 68 |
+
if selected_visit['blood_pressure'].values[0] > 140:
|
| 69 |
+
st.error('High blood pressure detected!')
|
| 70 |
+
|
| 71 |
+
# Download Button for Patient Data
|
| 72 |
+
st.download_button(
|
| 73 |
+
label="Download Patient Data as CSV",
|
| 74 |
+
data=patient_data.to_csv().encode('utf-8'),
|
| 75 |
+
file_name=f'patient_{patient_id}_data.csv',
|
| 76 |
+
mime='text/csv',
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
elif choice == "Add New Patient":
|
| 80 |
+
st.header("Add New Patient")
|
| 81 |
+
|
| 82 |
+
# Input fields for new patient data
|
| 83 |
+
new_patient_id = st.number_input("Patient ID", min_value=0, step=1)
|
| 84 |
+
new_name = st.text_input("Name")
|
| 85 |
+
new_age = st.number_input("Age", min_value=0, step=1)
|
| 86 |
+
new_gender = st.selectbox("Gender", ["Male", "Female"])
|
| 87 |
+
new_medical_history = st.text_area("Medical History")
|
| 88 |
+
|
| 89 |
+
if st.button("Add Patient"):
|
| 90 |
+
new_patient_data = {
|
| 91 |
+
'patient_id': new_patient_id,
|
| 92 |
+
'name': new_name,
|
| 93 |
+
'age': new_age,
|
| 94 |
+
'gender': new_gender,
|
| 95 |
+
'medical_history': new_medical_history,
|
| 96 |
+
'date': None,
|
| 97 |
+
'heart_rate': None,
|
| 98 |
+
'blood_pressure': None,
|
| 99 |
+
'blood_glucose': None,
|
| 100 |
+
'temperature': None,
|
| 101 |
+
'medical_complaints': None,
|
| 102 |
+
'symptoms': None,
|
| 103 |
+
'physical_examination': None,
|
| 104 |
+
'diagnosis': None,
|
| 105 |
+
'extra_notes': None,
|
| 106 |
+
'treatment': None
|
| 107 |
+
}
|
| 108 |
+
df = pd.concat([df, pd.DataFrame([new_patient_data])], ignore_index=True)
|
| 109 |
+
save_data(df)
|
| 110 |
+
st.success("New patient added successfully!")
|
| 111 |
+
|
| 112 |
+
elif choice == "Add New Visit":
|
| 113 |
+
st.header("Add New Visit")
|
| 114 |
+
|
| 115 |
+
# Input fields for adding a new visit
|
| 116 |
+
patient_id = st.number_input("Patient ID", min_value=0, step=1)
|
| 117 |
+
visit_date = st.date_input("Date of Visit", value=datetime.today())
|
| 118 |
+
medical_complaints = st.text_area("Medical Complaints")
|
| 119 |
+
symptoms = st.text_area("Symptoms")
|
| 120 |
+
physical_examination = st.text_area("Physical Examination")
|
| 121 |
+
diagnosis = st.text_area("Diagnosis")
|
| 122 |
+
heart_rate = st.number_input("Heart Rate", min_value=0)
|
| 123 |
+
blood_pressure = st.number_input("Blood Pressure", min_value=0)
|
| 124 |
+
temperature = st.number_input("Temperature", min_value=0)
|
| 125 |
+
glucose = st.number_input("Blood Glucose", min_value=0)
|
| 126 |
+
extra_notes = st.text_area("Extra Notes")
|
| 127 |
+
treatment = st.text_area("Treatment")
|
| 128 |
+
|
| 129 |
+
if st.button("Add Visit"):
|
| 130 |
+
new_visit_data = {
|
| 131 |
+
'patient_id': patient_id,
|
| 132 |
+
'name': df[df['patient_id'] == patient_id]['name'].values[0],
|
| 133 |
+
'age': df[df['patient_id'] == patient_id]['age'].values[0],
|
| 134 |
+
'gender': df[df['patient_id'] == patient_id]['gender'].values[0],
|
| 135 |
+
'medical_history': df[df['patient_id'] == patient_id]['medical_history'].values[0],
|
| 136 |
+
'date': visit_date.strftime('%Y-%m-%d'),
|
| 137 |
+
'heart_rate': heart_rate,
|
| 138 |
+
'blood_pressure': blood_pressure,
|
| 139 |
+
'blood_glucose': glucose,
|
| 140 |
+
'temperature': temperature,
|
| 141 |
+
'medical_complaints': medical_complaints,
|
| 142 |
+
'symptoms': symptoms,
|
| 143 |
+
'physical_examination': physical_examination,
|
| 144 |
+
'diagnosis': diagnosis,
|
| 145 |
+
'extra_notes': extra_notes,
|
| 146 |
+
'treatment': treatment
|
| 147 |
+
}
|
| 148 |
+
df = pd.concat([df, pd.DataFrame([new_visit_data])], ignore_index=True)
|
| 149 |
+
save_data(df)
|
| 150 |
+
st.success("New visit added successfully!")
|