Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,102 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# ----------------------------
|
| 11 |
-
st.set_page_config(page_title="Habit & To-Do Tracker", layout="wide")
|
| 12 |
-
st.title("π Habit & To-Do Tracker")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
# File Storage
|
| 16 |
-
# ----------------------------
|
| 17 |
-
DATA_FILE = "data.json"
|
| 18 |
|
| 19 |
-
|
| 20 |
if os.path.exists(DATA_FILE):
|
| 21 |
with open(DATA_FILE, "r") as f:
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def save_data(data):
|
| 26 |
-
with open(DATA_FILE, "w") as f:
|
| 27 |
-
json.dump(data, f)
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
st.sidebar.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
st.sidebar.success("Task added!")
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
if st.sidebar.button("Mark Habit Done") and habit_name.strip():
|
| 53 |
-
today = datetime.today().strftime("%Y-%m-%d")
|
| 54 |
-
if habit_name not in data["habits"]:
|
| 55 |
-
data["habits"][habit_name] = []
|
| 56 |
-
if today not in data["habits"][habit_name]:
|
| 57 |
-
data["habits"][habit_name].append(today)
|
| 58 |
-
save_data(data)
|
| 59 |
-
st.sidebar.success("Habit marked for today!")
|
| 60 |
else:
|
| 61 |
-
st.
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
st.subheader("
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
streak = 0
|
| 82 |
-
# Calculate consecutive days streak ending today
|
| 83 |
-
today = datetime.today().date()
|
| 84 |
-
for i in range(21):
|
| 85 |
-
day = today - timedelta(days=i)
|
| 86 |
-
if day.strftime("%Y-%m-%d") in dates_sorted:
|
| 87 |
-
streak += 1
|
| 88 |
-
else:
|
| 89 |
-
break
|
| 90 |
-
habit_type = "π Permanent Habit" if streak >= 21 else "β³ Temporary Habit"
|
| 91 |
-
habit_data.append({"Habit": habit, "Days Followed": len(dates), "Streak": streak, "Type": habit_type})
|
| 92 |
-
|
| 93 |
-
if habit_data:
|
| 94 |
-
habit_df = pd.DataFrame(habit_data)
|
| 95 |
-
st.dataframe(habit_df, use_container_width=True)
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
st.subheader("π Daily Report")
|
| 110 |
-
today_str = datetime.today().strftime("%Y-%m-%d")
|
| 111 |
-
today_tasks = [t for t in data["tasks"] if t["Date"] == today_str]
|
| 112 |
-
report_df = pd.DataFrame([{
|
| 113 |
-
"Date": today_str,
|
| 114 |
-
"Total Tasks Today": len(today_tasks),
|
| 115 |
-
"Completed Tasks Today": len([t for t in today_tasks if t["Completed"]]),
|
| 116 |
-
"Total Habits": len(data["habits"])
|
| 117 |
-
}])
|
| 118 |
-
st.table(report_df)
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
csv_data = report_df.to_csv(index=False).encode("utf-8")
|
| 124 |
-
st.download_button("β¬ Download CSV", csv_data, "daily_report.csv", "text/csv")
|
|
|
|
| 1 |
+
def main():
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import json
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="Habit & To-Do Tracker", layout="wide")
|
| 10 |
+
st.title("π Habit & To-Do Tracker")
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
DATA_FILE = "data.json"
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Load data
|
| 15 |
if os.path.exists(DATA_FILE):
|
| 16 |
with open(DATA_FILE, "r") as f:
|
| 17 |
+
data = json.load(f)
|
| 18 |
+
else:
|
| 19 |
+
data = {"tasks": [], "habits": {}}
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Sidebar: Add Task
|
| 22 |
+
st.sidebar.header("β Add Task")
|
| 23 |
+
task_name = st.sidebar.text_input("Task Name")
|
| 24 |
+
task_completed = st.sidebar.checkbox("Completed")
|
| 25 |
+
if st.sidebar.button("Add Task") and task_name.strip():
|
| 26 |
+
today = datetime.today().strftime("%Y-%m-%d")
|
| 27 |
+
data["tasks"].append({
|
| 28 |
+
"Task": task_name,
|
| 29 |
+
"Completed": task_completed,
|
| 30 |
+
"Date": today
|
| 31 |
+
})
|
| 32 |
+
with open(DATA_FILE, "w") as f:
|
| 33 |
+
json.dump(data, f)
|
| 34 |
+
st.sidebar.success("Task added!")
|
| 35 |
|
| 36 |
+
# Sidebar: Track Habit
|
| 37 |
+
st.sidebar.header("π₯ Track Habit")
|
| 38 |
+
habit_name = st.sidebar.text_input("Habit Name")
|
| 39 |
+
if st.sidebar.button("Mark Habit Done") and habit_name.strip():
|
| 40 |
+
today = datetime.today().strftime("%Y-%m-%d")
|
| 41 |
+
if habit_name not in data["habits"]:
|
| 42 |
+
data["habits"][habit_name] = []
|
| 43 |
+
if today not in data["habits"][habit_name]:
|
| 44 |
+
data["habits"][habit_name].append(today)
|
| 45 |
+
with open(DATA_FILE, "w") as f:
|
| 46 |
+
json.dump(data, f)
|
| 47 |
+
st.sidebar.success("Habit marked for today!")
|
| 48 |
+
else:
|
| 49 |
+
st.sidebar.warning("Already marked today.")
|
|
|
|
| 50 |
|
| 51 |
+
# Display Tasks
|
| 52 |
+
st.subheader("π Tasks")
|
| 53 |
+
if data["tasks"]:
|
| 54 |
+
task_df = pd.DataFrame(data["tasks"])
|
| 55 |
+
st.dataframe(task_df, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
else:
|
| 57 |
+
st.info("No tasks yet.")
|
| 58 |
|
| 59 |
+
# Display Habits
|
| 60 |
+
st.subheader("π₯ Habits")
|
| 61 |
+
habit_data = []
|
| 62 |
+
for habit, dates in data["habits"].items():
|
| 63 |
+
dates_sorted = sorted(dates)
|
| 64 |
+
streak = 0
|
| 65 |
+
today = datetime.today().date()
|
| 66 |
+
for i in range(21):
|
| 67 |
+
day = today - timedelta(days=i)
|
| 68 |
+
if day.strftime("%Y-%m-%d") in dates_sorted:
|
| 69 |
+
streak += 1
|
| 70 |
+
else:
|
| 71 |
+
break
|
| 72 |
+
habit_type = "π Permanent Habit" if streak >= 21 else "β³ Temporary Habit"
|
| 73 |
+
habit_data.append({"Habit": habit, "Days Followed": len(dates), "Streak": streak, "Type": habit_type})
|
| 74 |
|
| 75 |
+
if habit_data:
|
| 76 |
+
habit_df = pd.DataFrame(habit_data)
|
| 77 |
+
st.dataframe(habit_df, use_container_width=True)
|
| 78 |
+
st.subheader("π Habit Analytics")
|
| 79 |
+
st.plotly_chart(px.bar(habit_df, x="Habit", y="Days Followed", color="Type"), use_container_width=True)
|
| 80 |
+
st.plotly_chart(px.pie(habit_df, names="Habit", values="Days Followed"), use_container_width=True)
|
| 81 |
+
else:
|
| 82 |
+
st.info("No habits tracked yet.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Daily Report
|
| 85 |
+
st.subheader("π Daily Report")
|
| 86 |
+
today_str = datetime.today().strftime("%Y-%m-%d")
|
| 87 |
+
today_tasks = [t for t in data["tasks"] if t["Date"] == today_str]
|
| 88 |
+
report_df = pd.DataFrame([{
|
| 89 |
+
"Date": today_str,
|
| 90 |
+
"Total Tasks Today": len(today_tasks),
|
| 91 |
+
"Completed Tasks Today": len([t for t in today_tasks if t["Completed"]]),
|
| 92 |
+
"Total Habits": len(data["habits"])
|
| 93 |
+
}])
|
| 94 |
+
st.table(report_df)
|
| 95 |
|
| 96 |
+
# Download CSV
|
| 97 |
+
csv_data = report_df.to_csv(index=False).encode("utf-8")
|
| 98 |
+
st.download_button("β¬ Download CSV", csv_data, "daily_report.csv", "text/csv")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Run the app
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
main()
|
|
|
|
|
|