build a to do app
Drop files here
or click to upload
import streamlit as st
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime
from sqlalchemy.orm import Session, DeclarativeBase
from datetime import datetime
import time
# Page configuration
st.set_page_config(
page_title="To-Do App",
page_icon="✅",
layout="centered"
)
# Define database models
class Base(DeclarativeBase):
pass
class Todo(Base):
__tablename__ = "todos"
id = Column(Integer, primary_key=True)
title = Column(String, nullable=False)
description = Column(String, nullable=True)
completed = Column(Boolean, default=False)
created_at = Column(DateTime, default=datetime.now)
priority = Column(String, default="Medium")
# Create database engine and tables
engine = create_engine("sqlite:///todos.sqlite")
Base.metadata.create_all(bind=engine)
# App header
st.title("✅ To-Do App")
st.markdown("Manage your tasks efficiently with this simple to-do app.")
# Sidebar for adding new tasks
with st.sidebar:
st.header("Add New Task")
# Task input form
with st.form(key="add_task_form"):
task_title = st.text_input("Task Title", max_chars=100)
task_description = st.text_area("Description (Optional)", max_chars=500)
task_priority = st.selectbox("Priority", ["Low", "Medium", "High"])
submit_button = st.form_submit_button(label="Add Task")
if submit_button and task_title:
with Session(engine) as session:
new_todo = Todo(
title=task_title,
description=task_description,
priority=task_priority
)
session.add(new_todo)
session.commit()
st.success("Task added successfully!")
time.sleep(1)
st.rerun()
# Main content - Display tasks
task_tabs = st.tabs(["All Tasks", "Active", "Completed"])
# Function to display tasks in a tab
def display_tasks(tasks):
if not tasks:
st.info("No tasks to display.")
return
for task in tasks:
col1, col2, col3 = st.columns([0.05, 0.8, 0.15])
with col1:
checked = st.checkbox("", task.completed, key=f"check_{task.id}")
if checked != task.completed:
with Session(engine) as session:
todo = session.get(Todo, task.id)
todo.completed = checked
session.commit()
st.rerun()
with col2:
expander_label = task.title
if task.completed:
expander_label = f"~~{expander_label}~~"
# Add priority indicator
priority_color = {"Low": "🔵", "Medium": "🟢", "High": "🔴"}
expander_label = f"{priority_color.get(task.priority, '⚪')} {expander_label}"
with st.expander(expander_label):
if task.description:
st.write(task.description)
st.caption(f"Created: {task.created_at.strftime('%Y-%m-%d %H:%M')}")
st.caption(f"Priority: {task.priority}")
with col3:
if st.button("Delete", key=f"delete_{task.id}"):
with Session(engine) as session:
todo = session.get(Todo, task.id)
session.delete(todo)
session.commit()
st.rerun()
# Load and display tasks in tabs
with Session(engine) as session:
all_tasks = session.query(Todo).order_by(Todo.created_at.desc()).all()
active_tasks = [task for task in all_tasks if not task.completed]
completed_tasks = [task for task in all_tasks if task.completed]
with task_tabs[0]:
display_tasks(all_tasks)
with task_tabs[1]:
display_tasks(active_tasks)
with task_tabs[2]:
display_tasks(completed_tasks)
# Clear completed tasks button
if completed_tasks:
if st.button("Clear Completed Tasks"):
with Session(engine) as session:
session.query(Todo).filter(Todo.completed == True).delete()
session.commit()
st.success("Completed tasks cleared successfully!")
time.sleep(1)
st.rerun()
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?