import streamlit as st from datetime import date import matplotlib.pyplot as plt st.set_page_config(page_title="Smart Habit Coach", page_icon="🧠") st.title("🧠 Smart Habit Coach") today = str(date.today()) # ---------- Session State ---------- if "habits" not in st.session_state: st.session_state.habits = {} if "habit_input" not in st.session_state: st.session_state.habit_input = "" # ---------- Add Habit ---------- st.subheader("➕ Add Habit") def add_habit(): habit = st.session_state.habit_input.strip().lower() if habit: if habit not in st.session_state.habits: st.session_state.habits[habit] = { "streak": 0, "last_done": "" } st.session_state.habit_input = "" st.text_input("Enter habit name", key="habit_input") st.button("Add Habit", on_click=add_habit) # ---------- Daily Check-in ---------- st.subheader("✅ Daily Check-in") if st.session_state.habits: selected = st.selectbox("Select habit", list(st.session_state.habits.keys())) def mark_done(): habit_data = st.session_state.habits[selected] if habit_data["last_done"] == today: st.warning("Already completed today!") else: habit_data["streak"] += 1 habit_data["last_done"] = today st.button("Mark Done Today", on_click=mark_done) # ---------- Show Habits ---------- st.subheader("📋 Your Habits") if st.session_state.habits: for h, data in st.session_state.habits.items(): status = "✔" if data["last_done"] == today else "❌" st.write(f"{status} {h} | 🔥 Streak: {data['streak']}") # ---------- Progress ---------- st.subheader("📊 Progress") total = len(st.session_state.habits) done = sum(1 for v in st.session_state.habits.values() if v["last_done"] == today) if total > 0: st.progress(done / total) st.write(f"{done}/{total} habits completed today") # ---------- Better Smart Feedback ---------- st.subheader("🧠 Daily Feedback") if total > 0: if done == total: st.success("Excellent! You completed all habits today 🎉") elif done >= total / 2: st.info("Good progress 👍 Try to complete remaining habits.") else: st.warning("Low consistency today. Focus on your habits 💪") # ---------- Chart (Matplotlib) ---------- st.subheader("📊 Habit Completion Chart") if st.session_state.habits: labels = list(st.session_state.habits.keys()) values = [1 if v["last_done"] == today else 0 for v in st.session_state.habits.values()] fig, ax = plt.subplots() ax.bar(labels, values) ax.set_ylabel("Status (1 = Done, 0 = Not Done)") ax.set_title("Today's Habit Completion") st.pyplot(fig)