Spaces:
Sleeping
Sleeping
Deploy from anycoder
Browse files- app.py +51 -0
- requirements.txt +1 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
todos = []
|
| 4 |
+
|
| 5 |
+
def get_todos_data():
|
| 6 |
+
data = []
|
| 7 |
+
for i, todo in enumerate(todos):
|
| 8 |
+
status = "✅ Done" if todo["done"] else "⬜ Pending"
|
| 9 |
+
data.append([i, todo["task"], status, "❌ Delete"])
|
| 10 |
+
return data
|
| 11 |
+
|
| 12 |
+
def add_task(task):
|
| 13 |
+
if task and task.strip():
|
| 14 |
+
todos.append({"task": task.strip(), "done": False})
|
| 15 |
+
return get_todos_data(), ""
|
| 16 |
+
|
| 17 |
+
def handle_interaction(evt: gr.SelectData):
|
| 18 |
+
if evt.index[1] == 2:
|
| 19 |
+
index = evt.index[0]
|
| 20 |
+
todos[index]["done"] = not todos[index]["done"]
|
| 21 |
+
elif evt.index[1] == 3:
|
| 22 |
+
index = evt.index[0]
|
| 23 |
+
todos.pop(index)
|
| 24 |
+
return get_todos_data()
|
| 25 |
+
|
| 26 |
+
with gr.Blocks(title="Todo App") as app:
|
| 27 |
+
gr.Markdown("<h1>📝 Todo App</h1><p>Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a></p>")
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
task_input = gr.Textbox(
|
| 31 |
+
label="New Task",
|
| 32 |
+
placeholder="Enter a task and press Enter or click Add...",
|
| 33 |
+
scale=4
|
| 34 |
+
)
|
| 35 |
+
add_btn = gr.Button("Add Task", variant="primary", scale=1)
|
| 36 |
+
|
| 37 |
+
todo_table = gr.DataFrame(
|
| 38 |
+
headers=["ID", "Task", "Status (Click to Toggle)", "Action (Click to Delete)"],
|
| 39 |
+
datatype=["number", "str", "str", "str"],
|
| 40 |
+
value=get_todos_data,
|
| 41 |
+
interactive=False,
|
| 42 |
+
wrap=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
add_btn.click(add_task, inputs=[task_input], outputs=[todo_table, task_input])
|
| 46 |
+
task_input.submit(add_task, inputs=[task_input], outputs=[todo_table, task_input])
|
| 47 |
+
|
| 48 |
+
todo_table.select(handle_interaction, inputs=None, outputs=todo_table)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
app.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|