Spaces:
Runtime error
Runtime error
File size: 3,943 Bytes
f028ba0 135fbc1 f028ba0 c96d2c1 3980fcb 36bec21 c96d2c1 220cb53 207d922 c96d2c1 220cb53 135fbc1 c96d2c1 3980fcb 220cb53 135fbc1 c96d2c1 207d922 135fbc1 c96d2c1 36bec21 135fbc1 c96d2c1 207d922 c96d2c1 135fbc1 207d922 135fbc1 c96d2c1 220cb53 3980fcb f028ba0 220cb53 f028ba0 36bec21 3980fcb 207d922 c96d2c1 f028ba0 c96d2c1 207d922 36bec21 135fbc1 36bec21 207d922 36bec21 207d922 c96d2c1 207d922 c96d2c1 36bec21 3980fcb 207d922 f028ba0 220cb53 f028ba0 36bec21 c96d2c1 36bec21 3980fcb 207d922 36bec21 207d922 c96d2c1 207d922 f028ba0 36bec21 f028ba0 36bec21 f028ba0 36bec21 f028ba0 36bec21 220cb53 f028ba0 36bec21 f028ba0 36bec21 207d922 3980fcb c96d2c1 f028ba0 220cb53 135fbc1 207d922 c96d2c1 135fbc1 207d922 3980fcb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 155 156 157 158 159 160 161 | import streamlit as st
import pandas as pd
from datetime import datetime
import os
import matplotlib.pyplot as plt
DATA_FILE = "habits.csv"
# ---------- Page Config ----------
st.set_page_config(page_title="Habit Tracker", layout="centered")
# ---------- CLEAN LIGHT GRAY UI ----------
st.markdown("""
<style>
.block-container {
padding-top: 2rem;
padding-bottom: 2rem;
background-color: #f3f4f6;
}
/* Global text visibility */
h1, h2, h3, p, div {
color: #0f172a !important;
}
/* Card */
.card {
background: #ffffff;
padding: 14px;
border-radius: 12px;
border-left: 5px solid #38bdf8;
box-shadow: 0 2px 10px rgba(0,0,0,0.06);
margin-bottom: 10px;
}
/* Streak */
.streak {
color: #b45309;
font-weight: 600;
}
/* Buttons */
.stButton > button {
background-color: #38bdf8;
color: white;
border-radius: 8px;
border: none;
}
.stButton > button:hover {
background-color: #0ea5e9;
}
/* Subtle spacing */
.css-1d391kg {
padding-top: 1rem;
}
</style>
""", unsafe_allow_html=True)
# ---------- DATA ----------
def load_data():
if os.path.exists(DATA_FILE):
return pd.read_csv(DATA_FILE)
return pd.DataFrame(columns=["Habit", "Streak", "Last Completed"])
def save_data(df):
df.to_csv(DATA_FILE, index=False)
if "habits" not in st.session_state:
st.session_state.habits = load_data()
df = st.session_state.habits
# ---------- TITLE ----------
st.title("Habit Tracker")
st.write("Build consistency through daily habits")
# ---------- ADD HABIT ----------
st.subheader("Add Habit")
col1, col2 = st.columns([4, 1])
with col1:
new_habit = st.text_input("Enter habit", label_visibility="collapsed")
with col2:
if st.button("Add"):
if new_habit.strip():
if new_habit not in df["Habit"].values:
df = pd.concat(
[df, pd.DataFrame([[new_habit, 0, "Never"]], columns=df.columns)],
ignore_index=True
)
st.session_state.habits = df
save_data(df)
st.success("Added successfully")
else:
st.warning("Habit already exists")
# ---------- HABITS ----------
st.subheader("Your Habits")
today = datetime.now().date()
if df.empty:
st.info("No habits added yet")
else:
for i, row in df.iterrows():
col1, col2, col3 = st.columns([5, 1, 1])
with col1:
st.markdown(f"""
<div class="card">
<div style="font-size:16px; font-weight:600;">
{row['Habit']}
</div>
<div class="streak">
Streak: {row['Streak']}
</div>
</div>
""", unsafe_allow_html=True)
with col2:
if st.button("Done", key=f"done_{i}"):
last_date = row["Last Completed"]
if last_date != "Never":
last_date = pd.to_datetime(last_date).date()
if (today - last_date).days == 1:
df.loc[i, "Streak"] += 1
elif (today - last_date).days > 1:
df.loc[i, "Streak"] = 1
else:
df.loc[i, "Streak"] = 1
df.loc[i, "Last Completed"] = str(today)
st.session_state.habits = df
save_data(df)
st.rerun()
with col3:
if st.button("Delete", key=f"del_{i}"):
df = df[df["Habit"] != row["Habit"]]
st.session_state.habits = df
save_data(df)
st.rerun()
# ---------- GRAPH ----------
st.subheader("Progress Overview")
if not df.empty:
fig, ax = plt.subplots()
ax.bar(df["Habit"], df["Streak"], color="#38bdf8")
ax.set_facecolor("#f3f4f6")
ax.set_ylabel("Streak")
plt.xticks(rotation=20)
st.pyplot(fig) |