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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# File to store tasks
|
| 7 |
+
TASKS_FILE = "tasks.json"
|
| 8 |
+
|
| 9 |
+
def load_tasks():
|
| 10 |
+
if os.path.exists(TASKS_FILE):
|
| 11 |
+
with open(TASKS_FILE, "r") as f:
|
| 12 |
+
return json.load(f)
|
| 13 |
+
return []
|
| 14 |
+
|
| 15 |
+
def save_tasks(tasks):
|
| 16 |
+
with open(TASKS_FILE, "w") as f:
|
| 17 |
+
json.dump(tasks, f)
|
| 18 |
+
|
| 19 |
+
def add_task(task_name, task_description, task_priority, task_due_date):
|
| 20 |
+
tasks = load_tasks()
|
| 21 |
+
new_task = {
|
| 22 |
+
"id": len(tasks) + 1,
|
| 23 |
+
"name": task_name,
|
| 24 |
+
"description": task_description,
|
| 25 |
+
"priority": task_priority,
|
| 26 |
+
"due_date": task_due_date,
|
| 27 |
+
"completed": False,
|
| 28 |
+
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 29 |
+
}
|
| 30 |
+
tasks.append(new_task)
|
| 31 |
+
save_tasks(tasks)
|
| 32 |
+
return "", "", "normal", None, update_task_list()
|
| 33 |
+
|
| 34 |
+
def update_task_list(filter_status="all", filter_priority="all"):
|
| 35 |
+
tasks = load_tasks()
|
| 36 |
+
|
| 37 |
+
if filter_status != "all":
|
| 38 |
+
completed = filter_status == "completed"
|
| 39 |
+
tasks = [task for task in tasks if task["completed"] == completed]
|
| 40 |
+
|
| 41 |
+
if filter_priority != "all":
|
| 42 |
+
tasks = [task for task in tasks if task["priority"] == filter_priority]
|
| 43 |
+
|
| 44 |
+
if not tasks:
|
| 45 |
+
return "No tasks found."
|
| 46 |
+
|
| 47 |
+
html_content = "<div style='max-height: 400px; overflow-y: auto;'>"
|
| 48 |
+
for task in tasks:
|
| 49 |
+
status_class = "line-through text-gray-500" if task["completed"] else ""
|
| 50 |
+
priority_color = {
|
| 51 |
+
"low": "bg-green-100 text-green-800",
|
| 52 |
+
"normal": "bg-blue-100 text-blue-800",
|
| 53 |
+
"high": "bg-red-100 text-red-800"
|
| 54 |
+
}.get(task["priority"], "bg-gray-100 text-gray-800")
|
| 55 |
+
|
| 56 |
+
due_date = task["due_date"] if task["due_date"] else "No due date"
|
| 57 |
+
|
| 58 |
+
html_content += f"""
|
| 59 |
+
<div class="border rounded-lg p-4 mb-3 bg-white shadow-sm">
|
| 60 |
+
<div class="flex justify-between items-start">
|
| 61 |
+
<div class="flex-1">
|
| 62 |
+
<h3 class="font-semibold text-lg {status_class}">{task['name']}</h3>
|
| 63 |
+
<p class="text-gray-600 mt-1">{task['description']}</p>
|
| 64 |
+
<div class="flex flex-wrap gap-2 mt-2">
|
| 65 |
+
<span class="px-2 py-1 rounded-full text-xs font-medium {priority_color}">{task['priority'].capitalize()}</span>
|
| 66 |
+
<span class="px-2 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-800">{due_date}</span>
|
| 67 |
+
<span class="px-2 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-800">Created: {task['created_at']}</span>
|
| 68 |
+
</div>
|
| 69 |
+
</div>
|
| 70 |
+
<div class="flex gap-2 ml-4">
|
| 71 |
+
<button onclick="completeTask({task['id']})" class="px-3 py-1 bg-green-500 text-white rounded hover:bg-green-600 transition">✓</button>
|
| 72 |
+
<button onclick="deleteTask({task['id']})" class="px-3 py-1 bg-red-500 text-white rounded hover:bg-red-600 transition">×</button>
|
| 73 |
+
</div>
|
| 74 |
+
</div>
|
| 75 |
+
</div>
|
| 76 |
+
"""
|
| 77 |
+
html_content += "</div>"
|
| 78 |
+
return html_content
|
| 79 |
+
|
| 80 |
+
def toggle_task_completion(task_id):
|
| 81 |
+
tasks = load_tasks()
|
| 82 |
+
for task in tasks:
|
| 83 |
+
if task["id"] == task_id:
|
| 84 |
+
task["completed"] = not task["completed"]
|
| 85 |
+
break
|
| 86 |
+
save_tasks(tasks)
|
| 87 |
+
return update_task_list()
|
| 88 |
+
|
| 89 |
+
def delete_task(task_id):
|
| 90 |
+
tasks = load_tasks()
|
| 91 |
+
tasks = [task for task in tasks if task["id"] != task_id]
|
| 92 |
+
# Reassign IDs
|
| 93 |
+
for i, task in enumerate(tasks):
|
| 94 |
+
task["id"] = i + 1
|
| 95 |
+
save_tasks(tasks)
|
| 96 |
+
return update_task_list()
|
| 97 |
+
|
| 98 |
+
def clear_completed_tasks():
|
| 99 |
+
tasks = load_tasks()
|
| 100 |
+
tasks = [task for task in tasks if not task["completed"]]
|
| 101 |
+
# Reassign IDs
|
| 102 |
+
for i, task in enumerate(tasks):
|
| 103 |
+
task["id"] = i + 1
|
| 104 |
+
save_tasks(tasks)
|
| 105 |
+
return update_task_list()
|
| 106 |
+
|
| 107 |
+
with gr.Blocks(title="To-Do App", theme=gr.themes.Soft()) as demo:
|
| 108 |
+
gr.HTML("""
|
| 109 |
+
<div style="text-align: center; margin-bottom: 20px;">
|
| 110 |
+
<h1 style="font-size: 2em; margin-bottom: 10px;">📝 To-Do App</h1>
|
| 111 |
+
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="color: #666; text-decoration: none;">Built with anycoder</a>
|
| 112 |
+
</div>
|
| 113 |
+
""")
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column(scale=1):
|
| 117 |
+
task_name = gr.Textbox(label="Task Name", placeholder="Enter task name...")
|
| 118 |
+
task_description = gr.Textbox(label="Description", placeholder="Enter task description...", lines=3)
|
| 119 |
+
task_priority = gr.Radio(["low", "normal", "high"], label="Priority", value="normal")
|
| 120 |
+
task_due_date = gr.DateTime(label="Due Date (Optional)", include_time=True)
|
| 121 |
+
add_btn = gr.Button("Add Task", variant="primary")
|
| 122 |
+
|
| 123 |
+
with gr.Column(scale=2):
|
| 124 |
+
with gr.Row():
|
| 125 |
+
filter_status = gr.Radio(["all", "active", "completed"], label="Filter by Status", value="all")
|
| 126 |
+
filter_priority = gr.Radio(["all", "low", "normal", "high"], label="Filter by Priority", value="all")
|
| 127 |
+
|
| 128 |
+
task_list = gr.HTML(label="Tasks", value=update_task_list())
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
clear_completed_btn = gr.Button("Clear Completed Tasks", variant="stop")
|
| 132 |
+
|
| 133 |
+
# JavaScript for handling button clicks
|
| 134 |
+
gr.HTML("""
|
| 135 |
+
<script>
|
| 136 |
+
function completeTask(taskId) {
|
| 137 |
+
const formData = new FormData();
|
| 138 |
+
formData.append('task_id', taskId);
|
| 139 |
+
|
| 140 |
+
fetch('/complete_task', {
|
| 141 |
+
method: 'POST',
|
| 142 |
+
body: formData
|
| 143 |
+
}).then(response => response.json()).then(data => {
|
| 144 |
+
document.querySelector('#task_list').innerHTML = data;
|
| 145 |
+
});
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
function deleteTask(taskId) {
|
| 149 |
+
const formData = new FormData();
|
| 150 |
+
formData.append('task_id', taskId);
|
| 151 |
+
|
| 152 |
+
fetch('/delete_task', {
|
| 153 |
+
method: 'POST',
|
| 154 |
+
body: formData
|
| 155 |
+
}).then(response => response.json()).then(data => {
|
| 156 |
+
document.querySelector('#task_list').innerHTML = data;
|
| 157 |
+
});
|
| 158 |
+
}
|
| 159 |
+
</script>
|
| 160 |
+
""")
|
| 161 |
+
|
| 162 |
+
# Event handlers
|
| 163 |
+
add_btn.click(
|
| 164 |
+
add_task,
|
| 165 |
+
inputs=[task_name, task_description, task_priority, task_due_date],
|
| 166 |
+
outputs=[task_name, task_description, task_priority, task_due_date, task_list]
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
filter_status.change(
|
| 170 |
+
update_task_list,
|
| 171 |
+
inputs=[filter_status, filter_priority],
|
| 172 |
+
outputs=task_list
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
filter_priority.change(
|
| 176 |
+
update_task_list,
|
| 177 |
+
inputs=[filter_status, filter_priority],
|
| 178 |
+
outputs=task_list
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
clear_completed_btn.click(
|
| 182 |
+
clear_completed_tasks,
|
| 183 |
+
outputs=task_list
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
# Add API endpoints for JavaScript calls
|
| 187 |
+
@demo.app.post("/complete_task")
|
| 188 |
+
async def api_complete_task(request):
|
| 189 |
+
form = await request.form()
|
| 190 |
+
task_id = int(form["task_id"])
|
| 191 |
+
result = toggle_task_completion(task_id)
|
| 192 |
+
return {"html": result}
|
| 193 |
+
|
| 194 |
+
@demo.app.post("/delete_task")
|
| 195 |
+
async def api_delete_task(request):
|
| 196 |
+
form = await request.form()
|
| 197 |
+
task_id = int(form["task_id"])
|
| 198 |
+
result = delete_task(task_id)
|
| 199 |
+
return {"html": result}
|
| 200 |
+
|
| 201 |
+
if __name__ == "__main__":
|
| 202 |
+
demo.launch()
|