New_To_Do_List / src /streamlit_app.py
Ahmad-01's picture
Update src/streamlit_app.py
5386bc4 verified
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")