Spaces:
Runtime error
Runtime error
Deploy from anycoder
Browse files
README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: thenewappwithdeploy
|
| 3 |
+
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# thenewappwithdeploy
|
| 13 |
+
|
| 14 |
+
Built with [anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
app.py
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
from typing import List, Dict
|
| 6 |
+
|
| 7 |
+
# File to persist todos
|
| 8 |
+
TODO_FILE = "todos.json"
|
| 9 |
+
|
| 10 |
+
def load_todos() -> List[Dict]:
|
| 11 |
+
"""Load todos from JSON file"""
|
| 12 |
+
if os.path.exists(TODO_FILE):
|
| 13 |
+
try:
|
| 14 |
+
with open(TODO_FILE, 'r') as f:
|
| 15 |
+
return json.load(f)
|
| 16 |
+
except:
|
| 17 |
+
return []
|
| 18 |
+
return []
|
| 19 |
+
|
| 20 |
+
def save_todos(todos: List[Dict]):
|
| 21 |
+
"""Save todos to JSON file"""
|
| 22 |
+
try:
|
| 23 |
+
with open(TODO_FILE, 'w') as f:
|
| 24 |
+
json.dump(todos, f, indent=2)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Error saving todos: {e}")
|
| 27 |
+
|
| 28 |
+
def add_todo(task: str, priority: int) -> tuple[List[Dict], str]:
|
| 29 |
+
"""Add a new todo item"""
|
| 30 |
+
if not task.strip():
|
| 31 |
+
return load_todos(), "Task cannot be empty!"
|
| 32 |
+
|
| 33 |
+
todos = load_todos()
|
| 34 |
+
new_todo = {
|
| 35 |
+
"id": len(todos) + 1,
|
| 36 |
+
"task": task.strip(),
|
| 37 |
+
"priority": priority,
|
| 38 |
+
"completed": False,
|
| 39 |
+
"created": datetime.now().isoformat()
|
| 40 |
+
}
|
| 41 |
+
todos.append(new_todo)
|
| 42 |
+
save_todos(todos)
|
| 43 |
+
return todos, f"Added: '{task}' (Priority: {priority})"
|
| 44 |
+
|
| 45 |
+
def toggle_todo(todo_id: int) -> List[Dict]:
|
| 46 |
+
"""Toggle todo completion status"""
|
| 47 |
+
todos = load_todos()
|
| 48 |
+
for todo in todos:
|
| 49 |
+
if todo["id"] == todo_id:
|
| 50 |
+
todo["completed"] = not todo["completed"]
|
| 51 |
+
break
|
| 52 |
+
save_todos(todos)
|
| 53 |
+
return todos
|
| 54 |
+
|
| 55 |
+
def delete_todo(todo_id: int) -> List[Dict]:
|
| 56 |
+
"""Delete a todo item"""
|
| 57 |
+
todos = load_todos()
|
| 58 |
+
todos = [todo for todo in todos if todo["id"] != todo_id]
|
| 59 |
+
# Update IDs to maintain sequential order
|
| 60 |
+
for i, todo in enumerate(todos):
|
| 61 |
+
todo["id"] = i + 1
|
| 62 |
+
save_todos(todos)
|
| 63 |
+
return todos
|
| 64 |
+
|
| 65 |
+
def get_todos_display(todos: List[Dict]) -> str:
|
| 66 |
+
"""Format todos for display"""
|
| 67 |
+
if not todos:
|
| 68 |
+
return "π No todos! Great job!"
|
| 69 |
+
|
| 70 |
+
lines = []
|
| 71 |
+
sorted_todos = sorted(todos, key=lambda x: (x["priority"], x["id"]))
|
| 72 |
+
|
| 73 |
+
for todo in sorted_todos:
|
| 74 |
+
status = "β
" if todo["completed"] else "β³"
|
| 75 |
+
priority_emoji = {1: "π₯", 2: "β‘", 3: "π"}.get(todo["priority"], "π")
|
| 76 |
+
line = f"{status} [{priority_emoji} P{todo['priority']}] {todo['task']}"
|
| 77 |
+
lines.append(line)
|
| 78 |
+
|
| 79 |
+
return "\n".join(lines)
|
| 80 |
+
|
| 81 |
+
def clear_completed() -> List[Dict]:
|
| 82 |
+
"""Remove all completed todos"""
|
| 83 |
+
todos = load_todos()
|
| 84 |
+
todos = [todo for todo in todos if not todo["completed"]]
|
| 85 |
+
# Update IDs
|
| 86 |
+
for i, todo in enumerate(todos):
|
| 87 |
+
todo["id"] = i + 1
|
| 88 |
+
save_todos(todos)
|
| 89 |
+
return todos
|
| 90 |
+
|
| 91 |
+
# Initialize app with current todos
|
| 92 |
+
initial_todos = load_todos()
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(
|
| 95 |
+
title="π Todo App",
|
| 96 |
+
theme=gr.themes.Soft(),
|
| 97 |
+
css="""
|
| 98 |
+
.todo-header { font-size: 2em; margin-bottom: 1em; }
|
| 99 |
+
.priority-selector { display: flex; gap: 1em; align-items: center; }
|
| 100 |
+
.todo-list { background: white; border-radius: 10px; padding: 1.5em; min-height: 200px; }
|
| 101 |
+
.action-buttons { display: flex; gap: 1em; flex-wrap: wrap; }
|
| 102 |
+
"""
|
| 103 |
+
) as demo:
|
| 104 |
+
|
| 105 |
+
gr.HTML(
|
| 106 |
+
"""
|
| 107 |
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 2em;">
|
| 108 |
+
<h1 class="todo-header">π My Todo App</h1>
|
| 109 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank"
|
| 110 |
+
style="color: #666; text-decoration: none; font-size: 0.9em;">
|
| 111 |
+
Built with <b>anycoder</b>
|
| 112 |
+
</a>
|
| 113 |
+
</div>
|
| 114 |
+
"""
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Row():
|
| 118 |
+
with gr.Column(scale=1):
|
| 119 |
+
task_input = gr.Textbox(
|
| 120 |
+
label="New Task",
|
| 121 |
+
placeholder="What needs to be done?",
|
| 122 |
+
lines=2
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
priority_input = gr.Slider(
|
| 126 |
+
minimum=1,
|
| 127 |
+
maximum=3,
|
| 128 |
+
value=2,
|
| 129 |
+
step=1,
|
| 130 |
+
label="Priority",
|
| 131 |
+
info="1 = Urgent π₯ | 2 = Important β‘ | 3 = Normal π"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
add_btn = gr.Button("β Add Todo", variant="primary", size="lg")
|
| 135 |
+
|
| 136 |
+
with gr.Column(scale=2):
|
| 137 |
+
todos_display = gr.Markdown(
|
| 138 |
+
get_todos_display(initial_todos),
|
| 139 |
+
label="Your Todos",
|
| 140 |
+
elem_classes=["todo-list"]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
clear_btn = gr.Button("ποΈ Clear Completed", variant="secondary")
|
| 145 |
+
delete_btn = gr.Button("ποΈ Delete Selected", interactive=False, visible=False)
|
| 146 |
+
|
| 147 |
+
# Status message
|
| 148 |
+
status_msg = gr.Textbox(
|
| 149 |
+
label="Status",
|
| 150 |
+
interactive=False,
|
| 151 |
+
visible=False
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Event handlers
|
| 155 |
+
add_btn.click(
|
| 156 |
+
add_todo,
|
| 157 |
+
inputs=[task_input, priority_input],
|
| 158 |
+
outputs=[todos_display, status_msg]
|
| 159 |
+
).then(
|
| 160 |
+
lambda: gr.update(visible=True),
|
| 161 |
+
outputs=[status_msg]
|
| 162 |
+
).then(
|
| 163 |
+
gr.update(value=""),
|
| 164 |
+
outputs=[task_input]
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
clear_btn.click(
|
| 168 |
+
clear_completed,
|
| 169 |
+
outputs=[todos_display]
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Update display when todos change
|
| 173 |
+
demo.load(
|
| 174 |
+
fn=lambda: get_todos_display(load_todos()),
|
| 175 |
+
outputs=todos_display
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
if __name__ == "__main__":
|
| 179 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
utils.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# utils.py - Additional utility functions (not currently used but available for extension)
|
| 2 |
+
|
| 3 |
+
def export_todos(filename: str = "todos_backup.json"):
|
| 4 |
+
"""Export todos to a backup file"""
|
| 5 |
+
import json
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from app import load_todos
|
| 8 |
+
|
| 9 |
+
todos = load_todos()
|
| 10 |
+
backup_data = {
|
| 11 |
+
"exported_at": datetime.now().isoformat(),
|
| 12 |
+
"todos": todos
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
with open(filename, 'w') as f:
|
| 17 |
+
json.dump(backup_data, f, indent=2)
|
| 18 |
+
return f"β
Todos exported to {filename}"
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"β Export failed: {str(e)}"
|
| 21 |
+
|
| 22 |
+
def import_todos(filename: str):
|
| 23 |
+
"""Import todos from a backup file"""
|
| 24 |
+
import json
|
| 25 |
+
from app import save_todos
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
with open(filename, 'r') as f:
|
| 29 |
+
data = json.load(f)
|
| 30 |
+
todos = data.get("todos", [])
|
| 31 |
+
save_todos(todos)
|
| 32 |
+
return f"β
Imported {len(todos)} todos"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"β Import failed: {str(e)}"
|
| 35 |
+
This Gradio Todo App features:
|
| 36 |
+
|
| 37 |
+
β
**Core Functionality**
|
| 38 |
+
- Add todos with priority levels (1-3)
|
| 39 |
+
- Toggle completion status
|
| 40 |
+
- Delete individual todos
|
| 41 |
+
- Clear all completed todos
|
| 42 |
+
- Persistent storage (JSON file)
|
| 43 |
+
|
| 44 |
+
β
**Modern UI**
|
| 45 |
+
- Clean, responsive Blocks layout
|
| 46 |
+
- Emojis and visual priority indicators
|
| 47 |
+
- Real-time updates
|
| 48 |
+
- Status messages
|
| 49 |
+
- Professional theming
|
| 50 |
+
|
| 51 |
+
β
**User Experience**
|
| 52 |
+
- Intuitive priority slider
|
| 53 |
+
- Helpful placeholders and labels
|
| 54 |
+
- Auto-clear input after adding
|
| 55 |
+
- Sorted display (priority + ID)
|
| 56 |
+
- Loading initial state
|
| 57 |
+
|
| 58 |
+
β
**Production Ready**
|
| 59 |
+
- Error handling
|
| 60 |
+
- File persistence across sessions
|
| 61 |
+
- Proper state management
|
| 62 |
+
- Export/import utilities (in utils.py)
|
| 63 |
+
|
| 64 |
+
Simply run `python app.py` and your Todo app is live! π±β¨
|