Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +38 -10
app/main.py
CHANGED
|
@@ -40,23 +40,51 @@ def delete_task(task_id: str):
|
|
| 40 |
### --- Gradio frontend logic --- ###
|
| 41 |
API_URL = "http://localhost:7860"
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def get_tasks_ui():
|
| 44 |
-
|
| 45 |
-
return [f"{t['title']} - {'✔️' if t['completed'] else '❌'}" for t in r.json()]
|
| 46 |
|
| 47 |
def add_task_ui(title):
|
| 48 |
if not title.strip():
|
| 49 |
-
return "Title cannot be empty", get_tasks_ui()
|
| 50 |
requests.post(f"{API_URL}/tasks/", json={"title": title, "completed": False})
|
| 51 |
-
return "", get_tasks_ui()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("##
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
app = gr.mount_gradio_app(app, demo, path="/")
|
|
|
|
| 40 |
### --- Gradio frontend logic --- ###
|
| 41 |
API_URL = "http://localhost:7860"
|
| 42 |
|
| 43 |
+
def get_tasks_data():
|
| 44 |
+
response = requests.get(f"{API_URL}/tasks/")
|
| 45 |
+
return response.json()
|
| 46 |
+
|
| 47 |
+
def format_task(task):
|
| 48 |
+
return f"{task['id']} | {task['title']} - {'✔️' if task['completed'] else '❌'}"
|
| 49 |
+
|
| 50 |
def get_tasks_ui():
|
| 51 |
+
return [format_task(t) for t in get_tasks_data()]
|
|
|
|
| 52 |
|
| 53 |
def add_task_ui(title):
|
| 54 |
if not title.strip():
|
| 55 |
+
return "Title cannot be empty", get_tasks_ui(), gr.update(choices=get_tasks_ui())
|
| 56 |
requests.post(f"{API_URL}/tasks/", json={"title": title, "completed": False})
|
| 57 |
+
return "", get_tasks_ui(), gr.update(choices=get_tasks_ui())
|
| 58 |
+
|
| 59 |
+
def complete_task_ui(task_id_combo):
|
| 60 |
+
task_id = task_id_combo.split(" | ")[0]
|
| 61 |
+
requests.patch(f"{API_URL}/tasks/{task_id}", params={"completed": True})
|
| 62 |
+
return get_tasks_ui(), gr.update(choices=get_tasks_ui())
|
| 63 |
+
|
| 64 |
+
def delete_task_ui(task_id_combo):
|
| 65 |
+
task_id = task_id_combo.split(" | ")[0]
|
| 66 |
+
requests.delete(f"{API_URL}/tasks/{task_id}")
|
| 67 |
+
return get_tasks_ui(), gr.update(choices=get_tasks_ui())
|
| 68 |
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("## ✅ Task List Manager (FastAPI + Supabase)")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
task_input = gr.Textbox(label="New Task", placeholder="e.g., Write blog post")
|
| 74 |
+
add_button = gr.Button("Add Task")
|
| 75 |
+
|
| 76 |
+
task_selector = gr.Dropdown(label="Select a Task", choices=[])
|
| 77 |
+
with gr.Row():
|
| 78 |
+
complete_button = gr.Button("Mark as Completed")
|
| 79 |
+
delete_button = gr.Button("Delete Task")
|
| 80 |
+
|
| 81 |
+
task_display = gr.Textbox(label="All Tasks", lines=10)
|
| 82 |
|
| 83 |
+
# Button click logic
|
| 84 |
+
add_button.click(add_task_ui, inputs=task_input, outputs=[task_input, task_display, task_selector])
|
| 85 |
+
complete_button.click(complete_task_ui, inputs=task_selector, outputs=[task_display, task_selector])
|
| 86 |
+
delete_button.click(delete_task_ui, inputs=task_selector, outputs=[task_display, task_selector])
|
| 87 |
+
demo.load(lambda: (get_tasks_ui(), get_tasks_ui()), outputs=[task_display, task_selector])
|
| 88 |
|
| 89 |
+
# Mount Gradio at root path
|
| 90 |
app = gr.mount_gradio_app(app, demo, path="/")
|