Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +26 -51
src/streamlit_app.py
CHANGED
|
@@ -1,36 +1,19 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import json
|
| 3 |
-
import os
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
# =============================
|
| 7 |
-
#
|
| 8 |
# =============================
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
st.
|
| 12 |
-
|
| 13 |
-
layout="centered"
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
# =============================
|
| 17 |
-
# Load / Save Functions
|
| 18 |
-
# =============================
|
| 19 |
-
def load_data():
|
| 20 |
-
if os.path.exists(DATA_FILE):
|
| 21 |
-
with open(DATA_FILE, "r") as f:
|
| 22 |
-
return json.load(f)
|
| 23 |
-
return {"tasks": [], "habits": []}
|
| 24 |
-
|
| 25 |
-
def save_data(data):
|
| 26 |
-
with open(DATA_FILE, "w") as f:
|
| 27 |
-
json.dump(data, f)
|
| 28 |
-
|
| 29 |
-
data = load_data()
|
| 30 |
|
| 31 |
# =============================
|
| 32 |
# App Title and Navigation
|
| 33 |
# =============================
|
|
|
|
| 34 |
st.title("π To-Do List & Habit Tracker")
|
| 35 |
|
| 36 |
page = st.sidebar.radio("Navigation", ["To-Do List", "Habit Tracker"])
|
|
@@ -41,32 +24,28 @@ page = st.sidebar.radio("Navigation", ["To-Do List", "Habit Tracker"])
|
|
| 41 |
if page == "To-Do List":
|
| 42 |
st.header("π Your Tasks")
|
| 43 |
|
| 44 |
-
new_task = st.text_input("Add a new task")
|
| 45 |
|
| 46 |
if st.button("Add Task"):
|
| 47 |
if new_task.strip():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
st.experimental_rerun() # This works in latest Streamlit versions
|
| 51 |
|
| 52 |
st.subheader("Pending Tasks")
|
| 53 |
-
for i, task in enumerate(
|
| 54 |
if not task["completed"]:
|
| 55 |
col1, col2 = st.columns([0.8, 0.2])
|
| 56 |
with col1:
|
| 57 |
checked = st.checkbox(task["task"], key=f"task_{i}")
|
| 58 |
if checked:
|
| 59 |
-
|
| 60 |
-
save_data(data)
|
| 61 |
-
st.experimental_rerun()
|
| 62 |
with col2:
|
| 63 |
if st.button("Delete", key=f"delete_{i}"):
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
st.experimental_rerun()
|
| 67 |
|
| 68 |
st.subheader("Completed Tasks")
|
| 69 |
-
for task in
|
| 70 |
if task["completed"]:
|
| 71 |
st.write("β
", task["task"])
|
| 72 |
|
|
@@ -76,34 +55,30 @@ if page == "To-Do List":
|
|
| 76 |
if page == "Habit Tracker":
|
| 77 |
st.header("π₯ Daily Habit Tracker")
|
| 78 |
|
| 79 |
-
new_habit = st.text_input("Add a new habit")
|
| 80 |
|
| 81 |
if st.button("Add Habit"):
|
| 82 |
if new_habit.strip():
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
st.experimental_rerun()
|
| 86 |
|
| 87 |
today = datetime.now().strftime("%Y-%m-%d")
|
| 88 |
completed_today = 0
|
| 89 |
|
| 90 |
-
for i, habit in enumerate(
|
| 91 |
checked = today in habit["dates"]
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
if today in habit["dates"]:
|
| 99 |
-
data["habits"][i]["dates"].remove(today)
|
| 100 |
-
save_data(data)
|
| 101 |
|
| 102 |
-
if today in
|
| 103 |
completed_today += 1
|
| 104 |
|
| 105 |
-
if
|
| 106 |
-
progress = completed_today / len(
|
| 107 |
st.subheader("π Today's Progress")
|
| 108 |
st.progress(progress)
|
| 109 |
st.write(f"{int(progress * 100)}% habits completed today")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
# =============================
|
| 5 |
+
# Initialize session state
|
| 6 |
# =============================
|
| 7 |
+
if "tasks" not in st.session_state:
|
| 8 |
+
st.session_state.tasks = []
|
| 9 |
|
| 10 |
+
if "habits" not in st.session_state:
|
| 11 |
+
st.session_state.habits = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# =============================
|
| 14 |
# App Title and Navigation
|
| 15 |
# =============================
|
| 16 |
+
st.set_page_config(page_title="To-Do & Habit Tracker", layout="centered")
|
| 17 |
st.title("π To-Do List & Habit Tracker")
|
| 18 |
|
| 19 |
page = st.sidebar.radio("Navigation", ["To-Do List", "Habit Tracker"])
|
|
|
|
| 24 |
if page == "To-Do List":
|
| 25 |
st.header("π Your Tasks")
|
| 26 |
|
| 27 |
+
new_task = st.text_input("Add a new task", key="new_task")
|
| 28 |
|
| 29 |
if st.button("Add Task"):
|
| 30 |
if new_task.strip():
|
| 31 |
+
st.session_state.tasks.append({"task": new_task, "completed": False})
|
| 32 |
+
st.session_state.new_task = ""
|
|
|
|
| 33 |
|
| 34 |
st.subheader("Pending Tasks")
|
| 35 |
+
for i, task in enumerate(st.session_state.tasks):
|
| 36 |
if not task["completed"]:
|
| 37 |
col1, col2 = st.columns([0.8, 0.2])
|
| 38 |
with col1:
|
| 39 |
checked = st.checkbox(task["task"], key=f"task_{i}")
|
| 40 |
if checked:
|
| 41 |
+
st.session_state.tasks[i]["completed"] = True
|
|
|
|
|
|
|
| 42 |
with col2:
|
| 43 |
if st.button("Delete", key=f"delete_{i}"):
|
| 44 |
+
st.session_state.tasks.pop(i)
|
| 45 |
+
break # break to avoid index error
|
|
|
|
| 46 |
|
| 47 |
st.subheader("Completed Tasks")
|
| 48 |
+
for task in st.session_state.tasks:
|
| 49 |
if task["completed"]:
|
| 50 |
st.write("β
", task["task"])
|
| 51 |
|
|
|
|
| 55 |
if page == "Habit Tracker":
|
| 56 |
st.header("π₯ Daily Habit Tracker")
|
| 57 |
|
| 58 |
+
new_habit = st.text_input("Add a new habit", key="new_habit")
|
| 59 |
|
| 60 |
if st.button("Add Habit"):
|
| 61 |
if new_habit.strip():
|
| 62 |
+
st.session_state.habits.append({"habit": new_habit, "dates": []})
|
| 63 |
+
st.session_state.new_habit = ""
|
|
|
|
| 64 |
|
| 65 |
today = datetime.now().strftime("%Y-%m-%d")
|
| 66 |
completed_today = 0
|
| 67 |
|
| 68 |
+
for i, habit in enumerate(st.session_state.habits):
|
| 69 |
checked = today in habit["dates"]
|
| 70 |
|
| 71 |
+
new_checked = st.checkbox(habit["habit"], value=checked, key=f"habit_{i}")
|
| 72 |
+
if new_checked and today not in habit["dates"]:
|
| 73 |
+
st.session_state.habits[i]["dates"].append(today)
|
| 74 |
+
elif not new_checked and today in habit["dates"]:
|
| 75 |
+
st.session_state.habits[i]["dates"].remove(today)
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
if today in st.session_state.habits[i]["dates"]:
|
| 78 |
completed_today += 1
|
| 79 |
|
| 80 |
+
if st.session_state.habits:
|
| 81 |
+
progress = completed_today / len(st.session_state.habits)
|
| 82 |
st.subheader("π Today's Progress")
|
| 83 |
st.progress(progress)
|
| 84 |
st.write(f"{int(progress * 100)}% habits completed today")
|