Spaces:
Runtime error
Runtime error
File size: 12,660 Bytes
b6e792e | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | import gradio as gr
from typing import List, Dict, Tuple
import time
class TodoApp:
def __init__(self):
self.todos = []
def add_todo(self, todo_text: str, current_todos: List[Dict]) -> Tuple[List[Dict], str]:
"""Add a new todo item to the list."""
if not todo_text.strip():
raise gr.Warning("Please enter a todo item!")
new_todo = {
"id": len(current_todos) + 1,
"text": todo_text.strip(),
"completed": False,
"created_at": time.strftime("%Y-%m-%d %H:%M")
}
updated_todos = current_todos + [new_todo]
return updated_todos, ""
def toggle_todo(self, todo_indices: List[int], current_todos: List[Dict]) -> List[Dict]:
"""Toggle the completion status of selected todos."""
if not todo_indices:
return current_todos
updated_todos = current_todos.copy()
for idx in todo_indices:
if 0 <= idx < len(updated_todos):
updated_todos[idx]["completed"] = not updated_todos[idx]["completed"]
return updated_todos
def delete_todos(self, todo_indices: List[int], current_todos: List[Dict]) -> List[Dict]:
"""Delete selected todos from the list."""
if not todo_indices:
return current_todos
# Sort indices in reverse to avoid index shifting issues
todo_indices_sorted = sorted(todo_indices, reverse=True)
updated_todos = current_todos.copy()
for idx in todo_indices_sorted:
if 0 <= idx < len(updated_todos):
del updated_todos[idx]
# Reassign IDs
for i, todo in enumerate(updated_todos):
todo["id"] = i + 1
return updated_todos
def clear_all(self) -> List[Dict]:
"""Clear all todos from the list."""
return []
def get_todo_display(self, todos: List[Dict]) -> List[str]:
"""Format todos for display in CheckboxGroup."""
display_list = []
for todo in todos:
status = "β
" if todo["completed"] else "β"
display_text = f"{status} {todo['text']} (Created: {todo['created_at']})"
display_list.append(display_text)
return display_list
# Initialize the TodoApp
todo_manager = TodoApp()
def create_todo_app():
"""Create and configure the Gradio Todo App interface."""
with gr.Blocks(
title="Todo App",
theme=gr.themes.Soft(),
css="""
.todo-container {
border: 2px solid #e5e7eb;
border-radius: 8px;
padding: 16px;
margin: 8px 0;
background: #f9fafb;
}
.header-text {
text-align: center;
margin-bottom: 20px;
}
"""
) as demo:
# Header with attribution
gr.HTML("""
<div class="header-text">
<h1>π Todo App</h1>
<p>Manage your tasks efficiently with this simple todo application!</p>
<p style="font-size: 0.9em; color: #666;">
Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a>
</p>
</div>
""")
# State to manage the todo list
todo_state = gr.State(value=[])
with gr.Row():
with gr.Column(scale=3):
# Input section
with gr.Group():
gr.Markdown("### Add New Todo")
todo_input = gr.Textbox(
placeholder="Enter your todo item here...",
label="Todo Item",
lines=1,
max_lines=1,
autofocus=True,
show_label=False
)
with gr.Row():
add_btn = gr.Button(
"β Add Todo",
variant="primary",
size="sm"
)
clear_input_btn = gr.Button(
"ποΈ Clear Input",
variant="secondary",
size="sm"
)
with gr.Column(scale=2):
# Statistics
with gr.Group():
gr.Markdown("### Statistics")
total_count = gr.Number(
label="Total Todos",
value=0,
interactive=False
)
completed_count = gr.Number(
label="Completed",
value=0,
interactive=False
)
pending_count = gr.Number(
label="Pending",
value=0,
interactive=False
)
# Todo list display and actions
with gr.Group():
gr.Markdown("### Todo List")
todo_display = gr.CheckboxGroup(
choices=[],
label="Select todos to toggle or delete",
info="Check items to mark as complete/incomplete or delete them",
interactive=True
)
with gr.Row():
toggle_btn = gr.Button(
"π Toggle Selected",
variant="secondary",
size="sm"
)
delete_btn = gr.Button(
"ποΈ Delete Selected",
variant="stop",
size="sm"
)
clear_all_btn = gr.Button(
"π§Ή Clear All",
variant="stop",
size="sm"
)
# Hidden component to trigger updates
update_trigger = gr.Number(visible=False)
# Functions
def add_todo_handler(todo_text, current_todos):
"""Handle adding a new todo."""
updated_todos, cleared_input = todo_manager.add_todo(todo_text, current_todos)
display_choices = todo_manager.get_todo_display(updated_todos)
# Calculate statistics
total = len(updated_todos)
completed = sum(1 for todo in updated_todos if todo["completed"])
pending = total - completed
return (
updated_todos,
display_choices,
cleared_input,
total,
completed,
pending,
time.time() # Trigger update
)
def toggle_todo_handler(selected_indices, current_todos):
"""Handle toggling todo completion status."""
if not selected_indices:
return current_todos, todo_manager.get_todo_display(current_todos)
updated_todos = todo_manager.toggle_todo(selected_indices, current_todos)
display_choices = todo_manager.get_todo_display(updated_todos)
# Calculate statistics
total = len(updated_todos)
completed = sum(1 for todo in updated_todos if todo["completed"])
pending = total - completed
return (
updated_todos,
display_choices,
total,
completed,
pending,
time.time() # Trigger update
)
def delete_todo_handler(selected_indices, current_todos):
"""Handle deleting selected todos."""
if not selected_indices:
return current_todos, todo_manager.get_todo_display(current_todos)
updated_todos = todo_manager.delete_todos(selected_indices, current_todos)
display_choices = todo_manager.get_todo_display(updated_todos)
# Calculate statistics
total = len(updated_todos)
completed = sum(1 for todo in updated_todos if todo["completed"])
pending = total - completed
return (
updated_todos,
display_choices,
total,
completed,
pending,
time.time() # Trigger update
)
def clear_all_handler():
"""Handle clearing all todos."""
updated_todos = todo_manager.clear_all()
display_choices = todo_manager.get_todo_display(updated_todos)
return (
updated_todos,
display_choices,
0,
0,
0,
time.time() # Trigger update
)
def clear_input_handler():
"""Clear the input textbox."""
return ""
def update_display(current_todos):
"""Update the display when todos change."""
display_choices = todo_manager.get_todo_display(current_todos)
# Calculate statistics
total = len(current_todos)
completed = sum(1 for todo in current_todos if todo["completed"])
pending = total - completed
return display_choices, total, completed, pending
# Event handlers
add_btn.click(
fn=add_todo_handler,
inputs=[todo_input, todo_state],
outputs=[todo_state, todo_display, todo_input, total_count, completed_count, pending_count, update_trigger]
)
todo_input.submit(
fn=add_todo_handler,
inputs=[todo_input, todo_state],
outputs=[todo_state, todo_display, todo_input, total_count, completed_count, pending_count, update_trigger]
)
clear_input_btn.click(
fn=clear_input_handler,
outputs=[todo_input]
)
toggle_btn.click(
fn=toggle_todo_handler,
inputs=[todo_display, todo_state],
outputs=[todo_state, todo_display, total_count, completed_count, pending_count, update_trigger]
)
delete_btn.click(
fn=delete_todo_handler,
inputs=[todo_display, todo_state],
outputs=[todo_state, todo_display, total_count, completed_count, pending_count, update_trigger]
)
clear_all_btn.click(
fn=clear_all_handler,
outputs=[todo_state, todo_display, total_count, completed_count, pending_count, update_trigger]
)
# Update display when state changes
update_trigger.change(
fn=update_display,
inputs=[todo_state],
outputs=[todo_display, total_count, completed_count, pending_count]
)
# Examples
gr.Examples(
examples=[
["Buy groceries"],
["Finish project report"],
["Call mom"],
["Schedule dentist appointment"],
["Learn Gradio"]
],
inputs=[todo_input],
examples_per_page=3,
label="Example todos (click to use)"
)
# Instructions
with gr.Accordion("π How to Use", open=False):
gr.Markdown("""
### Instructions:
1. **Add Todo**: Type your task in the input box and press Enter or click "Add Todo"
2. **Mark Complete**: Check the box next to a todo and click "Toggle Selected"
3. **Delete Todos**: Select todos and click "Delete Selected"
4. **Clear All**: Click "Clear All" to remove all todos at once
5. **Statistics**: View real-time counts of total, completed, and pending todos
### Tips:
- β
indicates completed tasks
- β indicates pending tasks
- Each todo shows its creation time
- Select multiple todos to perform bulk actions
""")
return demo
# Create and launch the app
if __name__ == "__main__":
demo = create_todo_app()
demo.launch(
share=False,
show_error=True,
show_tips=True,
height=600,
width="100%"
) |