File size: 2,974 Bytes
dfbb3d9
9e20bdc
dfbb3d9
9e20bdc
bb69402
9e20bdc
bb69402
 
9e20bdc
bb69402
 
9e20bdc
 
 
 
bb69402
9e20bdc
 
 
 
 
 
 
 
 
 
5386bc4
 
9e20bdc
 
5386bc4
 
 
9e20bdc
 
bb69402
9e20bdc
 
 
5a9f516
 
bb69402
9e20bdc
 
bb69402
5386bc4
9e20bdc
 
bb69402
9e20bdc
 
 
 
 
 
 
 
 
5386bc4
9e20bdc
 
5386bc4
 
 
9e20bdc
 
 
 
bb69402
9e20bdc
 
bb69402
 
 
 
 
9e20bdc
bb69402
9e20bdc
 
bb69402
 
9e20bdc
 
 
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
import streamlit as st
from datetime import datetime

# =============================
# Initialize session state
# =============================
if "tasks" not in st.session_state:
    st.session_state.tasks = []

if "habits" not in st.session_state:
    st.session_state.habits = []

# =============================
# App Title and Navigation
# =============================
st.set_page_config(page_title="To-Do & Habit Tracker", layout="centered")
st.title("πŸ“ To-Do List & Habit Tracker")

page = st.sidebar.radio("Navigation", ["To-Do List", "Habit Tracker"])

# =============================
# TO-DO LIST SECTION
# =============================
if page == "To-Do List":
    st.header("πŸ“Œ Your Tasks")

    # Use temporary variable for input
    new_task = st.text_input("Add a new task")

    if st.button("Add Task"):
        if new_task.strip():
            st.session_state.tasks.append({"task": new_task.strip(), "completed": False})
            new_task = ""  # just clear local variable (widget keeps its value until next rerun)

    st.subheader("Pending Tasks")
    for i, task in enumerate(st.session_state.tasks):
        if not task["completed"]:
            col1, col2 = st.columns([0.8, 0.2])
            with col1:
                checked = st.checkbox(task["task"], key=f"task_{i}")
                if checked:
                    st.session_state.tasks[i]["completed"] = True
            with col2:
                if st.button("Delete", key=f"delete_{i}"):
                    st.session_state.tasks.pop(i)
                    break  # break to avoid index issues

    st.subheader("Completed Tasks")
    for task in st.session_state.tasks:
        if task["completed"]:
            st.write("βœ…", task["task"])

# =============================
# HABIT TRACKER SECTION
# =============================
if page == "Habit Tracker":
    st.header("πŸ”₯ Daily Habit Tracker")

    new_habit = st.text_input("Add a new habit")

    if st.button("Add Habit"):
        if new_habit.strip():
            st.session_state.habits.append({"habit": new_habit.strip(), "dates": []})
            new_habit = ""  # clear local variable

    today = datetime.now().strftime("%Y-%m-%d")
    completed_today = 0

    for i, habit in enumerate(st.session_state.habits):
        checked = today in habit["dates"]

        new_checked = st.checkbox(habit["habit"], value=checked, key=f"habit_{i}")
        if new_checked and today not in habit["dates"]:
            st.session_state.habits[i]["dates"].append(today)
        elif not new_checked and today in habit["dates"]:
            st.session_state.habits[i]["dates"].remove(today)

        if today in st.session_state.habits[i]["dates"]:
            completed_today += 1

    if st.session_state.habits:
        progress = completed_today / len(st.session_state.habits)
        st.subheader("πŸ“Š Today's Progress")
        st.progress(progress)
        st.write(f"{int(progress * 100)}% habits completed today")