MalikShehram's picture
Update app.py
5fedeb7 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from datetime import date
import os
# Page configuration
st.set_page_config(page_title="Therapist Dashboard", layout="wide")
# Utility functions for persistent storage
def load_data(file_name):
if os.path.exists(file_name):
return pd.read_csv(file_name)
else:
return pd.DataFrame()
def save_data(file_name, data):
data.to_csv(file_name, index=False)
# Load stored data
sessions_file = "sessions.csv"
moods_file = "moods.csv"
sessions_data = load_data(sessions_file)
moods_data = load_data(moods_file)
# Ensure necessary columns exist in loaded data
if not sessions_data.empty and "Patient" not in sessions_data.columns:
sessions_data["Patient"] = ""
if not moods_data.empty and "Patient" not in moods_data.columns:
moods_data["Patient"] = ""
# Title
st.title("Therapist Dashboard")
st.write("A comprehensive tool for therapists to manage sessions, track progress, and analyze patient data.")
# Sidebar Navigation
menu = st.sidebar.radio("Navigation", ["Home", "Session Tracker", "Mood Tracker", "Patient Analytics"])
if menu == "Home":
st.header("Welcome to the Therapist Dashboard")
st.write("This application provides tools to enhance your therapy practice. Navigate through the sidebar to explore features.")
elif menu == "Session Tracker":
st.header("Session Tracker")
# Log session
st.write("### Log New Session")
session_date = st.date_input("Session Date", value=date.today())
session_time = st.time_input("Session Time")
patient_name = st.text_input("Patient Name")
session_notes = st.text_area("Session Notes")
if st.button("Save Session"):
new_session = pd.DataFrame({
"Date": [session_date],
"Time": [session_time],
"Patient": [patient_name],
"Notes": [session_notes]
})
sessions_data = pd.concat([sessions_data, new_session], ignore_index=True)
save_data(sessions_file, sessions_data)
st.success(f"Session for {patient_name} on {session_date} saved successfully!")
# Display session logs
st.write("### Recent Sessions")
if not sessions_data.empty:
st.table(sessions_data.tail(10))
else:
st.write("No sessions logged yet.")
elif menu == "Mood Tracker":
st.header("Mood Tracker")
# Log mood
st.write("### Log Mood")
mood_date = st.date_input("Date", value=date.today(), key="mood_date")
mood_level = st.slider("Mood Level (1-10)", 1, 10, 5)
patient_name = st.text_input("Patient Name (for Mood Tracker)", key="mood_patient")
mood_notes = st.text_area("Notes", key="mood_notes")
if st.button("Save Mood Entry"):
new_mood = pd.DataFrame({
"Date": [mood_date],
"Mood Level": [mood_level],
"Patient": [patient_name],
"Notes": [mood_notes]
})
moods_data = pd.concat([moods_data, new_mood], ignore_index=True)
save_data(moods_file, moods_data)
st.success("Mood entry saved!")
# Display mood trends
st.write("### Mood Trends")
if not moods_data.empty:
moods_data["Date"] = pd.to_datetime(moods_data["Date"])
fig, ax = plt.subplots()
ax.plot(moods_data["Date"], moods_data["Mood Level"], marker="o")
ax.set_title("Mood Trends")
ax.set_xlabel("Date")
ax.set_ylabel("Mood Level")
st.pyplot(fig)
else:
st.write("No mood entries logged yet.")
elif menu == "Patient Analytics":
st.header("Patient Analytics")
if not sessions_data.empty:
st.write("### Session Analytics")
analytics_data = sessions_data.groupby("Patient").size().reset_index(name="Sessions Conducted")
if not moods_data.empty:
avg_mood = moods_data.groupby("Patient")["Mood Level"].mean().reset_index()
avg_mood.rename(columns={"Mood Level": "Average Mood"}, inplace=True)
analytics_data = pd.merge(analytics_data, avg_mood, on="Patient", how="left")
st.bar_chart(analytics_data.set_index("Patient"))
# Detailed patient reports
selected_patient = st.selectbox("Select a patient for detailed analytics", analytics_data["Patient"].unique())
patient_data = sessions_data[sessions_data["Patient"] == selected_patient]
st.write(f"### Detailed Report for {selected_patient}")
st.table(patient_data)
else:
st.write("No session data available for analytics.")