Spaces:
Runtime error
Runtime error
File size: 4,112 Bytes
f30fe32 | 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | import gradio as gr
from datetime import datetime
# In-memory storage for todos
todos = []
def add_todo(task, priority):
if not task.strip():
return todos_to_dataframe(), "β Please enter a task!"
todos.append({
"id": len(todos) + 1,
"task": task.strip(),
"priority": priority,
"completed": False,
"created": datetime.now().strftime("%Y-%m-%d %H:%M")
})
return todos_to_dataframe(), f"β
Added: {task}"
def toggle_complete(todo_id):
if not todo_id:
return todos_to_dataframe(), "β Please enter a valid ID"
try:
idx = int(todo_id) - 1
if 0 <= idx < len(todos):
todos[idx]["completed"] = not todos[idx]["completed"]
status = "completed" if todos[idx]["completed"] else "uncompleted"
return todos_to_dataframe(), f"β
Task marked as {status}"
return todos_to_dataframe(), "β Invalid ID"
except ValueError:
return todos_to_dataframe(), "β Please enter a valid number"
def delete_todo(todo_id):
if not todo_id:
return todos_to_dataframe(), "β Please enter a valid ID"
try:
idx = int(todo_id) - 1
if 0 <= idx < len(todos):
removed = todos.pop(idx)
for i, t in enumerate(todos):
t["id"] = i + 1
return todos_to_dataframe(), f"ποΈ Deleted: {removed['task']}"
return todos_to_dataframe(), "β Invalid ID"
except ValueError:
return todos_to_dataframe(), "β Please enter a valid number"
def clear_completed():
global todos
count = len([t for t in todos if t["completed"]])
todos = [t for t in todos if not t["completed"]]
for i, t in enumerate(todos):
t["id"] = i + 1
return todos_to_dataframe(), f"π§Ή Cleared {count} completed tasks"
def todos_to_dataframe():
if not todos:
return [["No tasks yet!", "", "", "", ""]]
return [[
t["id"],
"β
" if t["completed"] else "β¬",
t["task"],
{"High": "π΄", "Medium": "π‘", "Low": "π’"}[t["priority"]],
t["created"]
] for t in todos]
def get_stats():
total = len(todos)
completed = len([t for t in todos if t["completed"]])
pending = total - completed
return f"π Total: {total} | β
Completed: {completed} | β³ Pending: {pending}"
with gr.Blocks(title="Todo App", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# β¨ Simple Todo App
*[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)*
""")
stats = gr.Markdown(get_stats())
with gr.Row():
task_input = gr.Textbox(label="New Task", placeholder="Enter your task...", scale=3)
priority = gr.Dropdown(["High", "Medium", "Low"], value="Medium", label="Priority", scale=1)
add_btn = gr.Button("β Add", variant="primary", scale=1)
todo_table = gr.Dataframe(
headers=["ID", "Status", "Task", "Priority", "Created"],
value=todos_to_dataframe(),
interactive=False,
wrap=True
)
status_msg = gr.Textbox(label="Status", interactive=False)
with gr.Row():
todo_id = gr.Number(label="Task ID", precision=0)
toggle_btn = gr.Button("π Toggle Complete")
delete_btn = gr.Button("ποΈ Delete", variant="stop")
clear_btn = gr.Button("π§Ή Clear Completed")
def update_all(table, msg):
return table, msg, get_stats()
add_btn.click(add_todo, [task_input, priority], [todo_table, status_msg]).then(
lambda: ("", get_stats()), None, [task_input, stats])
task_input.submit(add_todo, [task_input, priority], [todo_table, status_msg]).then(
lambda: ("", get_stats()), None, [task_input, stats])
toggle_btn.click(toggle_complete, todo_id, [todo_table, status_msg]).then(
get_stats, None, stats)
delete_btn.click(delete_todo, todo_id, [todo_table, status_msg]).then(
get_stats, None, stats)
clear_btn.click(clear_completed, None, [todo_table, status_msg]).then(
get_stats, None, stats)
demo.launch() |