sam6309's picture
Create app.py
344f8b9 verified
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
# Initialize session state for storing mood logs
if "mood_data" not in st.session_state:
st.session_state.mood_data = pd.DataFrame(columns=["Date", "Mood", "Activity", "Notes"])
# App title and description
st.title("Mood Tracker")
st.write("A simple app to log and track your mood over time.")
# Sidebar for mood input
st.sidebar.header("Log Your Mood")
mood = st.sidebar.selectbox("How are you feeling today?", ["Happy", "Neutral", "Sad", "Anxious", "Excited", "Angry"])
activity = st.sidebar.text_input("What activity were you doing?")
notes = st.sidebar.text_area("Add any notes (optional):")
log_button = st.sidebar.button("Log Mood")
# Log the mood
if log_button:
new_entry = pd.DataFrame({
"Date": [dt.datetime.now()],
"Mood": [mood],
"Activity": [activity],
"Notes": [notes]
})
st.session_state.mood_data = pd.concat([st.session_state.mood_data, new_entry], ignore_index=True)
st.success("Mood logged successfully!")
# Display mood data
st.header("Mood History")
if not st.session_state.mood_data.empty:
st.dataframe(st.session_state.mood_data.sort_values(by="Date", ascending=False))
else:
st.write("No mood data logged yet. Use the sidebar to add entries.")
# Visualize mood trends
st.header("Mood Trends")
if not st.session_state.mood_data.empty:
# Convert date column to datetime for grouping
st.session_state.mood_data["Date"] = pd.to_datetime(st.session_state.mood_data["Date"])
mood_counts = st.session_state.mood_data.groupby(["Mood"]).size()
# Bar chart for mood distribution
fig, ax = plt.subplots()
mood_counts.plot(kind="bar", color="skyblue", ax=ax)
ax.set_title("Mood Distribution")
ax.set_ylabel("Count")
ax.set_xlabel("Mood")
st.pyplot(fig)
# Trend over time
mood_over_time = st.session_state.mood_data.groupby(st.session_state.mood_data["Date"].dt.date).size()
fig, ax = plt.subplots()
mood_over_time.plot(ax=ax, marker="o", color="orange")
ax.set_title("Mood Over Time")
ax.set_ylabel("Entries")
ax.set_xlabel("Date")
st.pyplot(fig)
else:
st.write("No data available for visualizations.")
# Resources for mental health
st.header("Mental Health Resources")
st.write("Here are some tips and resources to help improve your mental health:")
st.markdown("- Practice mindfulness and deep breathing.")
st.markdown("- Stay physically active.")
st.markdown("- Talk to a trusted friend or counselor.")
st.markdown("- [Find a therapist](https://www.psychologytoday.com)")