quick-bot-680 / utils.py
bingoweb's picture
Deploy Gradio app with multiple files
7eb7f6d verified
Raw History Blame Contribute Delete
2.43 kB
import numpy as np
import gradio as gr
from gradio.events import SelectData
# --- Data Handling Functions ---
def initial_state():
"""Returns the initial list of todo tasks."""
# Structure: List of tuples [(description: str, completed: bool)]
return [
("Buy groceries (milk, eggs)", False),
("Read Gradio documentation", True),
("Plan weekend trip", False),
]
def format_todos(todos: list[tuple[str, bool]]) -> list[list[str]]:
"""Converts the internal todo list structure into a format suitable for gr.Dataframe."""
if not todos:
return [["No tasks yet.", ""]]
formatted = []
for desc, completed in todos:
status = "✅ Done" if completed else "⬜ Pending"
formatted.append([desc, status])
return formatted
def add_task(todos: list[tuple[str, bool]], new_task: str):
"""Adds a new task to the todo list."""
if new_task and new_task.strip():
todos.append((new_task.strip(), False))
# Return updated state, updated dataframe display, and cleared input box
return todos, format_todos(todos), gr.Textbox(value="")
def _get_selected_index(todos: list[tuple[str, bool]], evt: SelectData) -> int | None:
"""Helper to safely retrieve the selected index from event data."""
if not todos or evt.value[0] == "No tasks yet.":
return None
# Assuming the selection is always a single row (index[0] gives row index)
if evt.index is None or len(evt.index) < 1:
return None
index = evt.index[0]
return index if 0 <= index < len(todos) else None
def toggle_complete(todos: list[tuple[str, bool]], evt: SelectData):
"""Toggles the completion status of the selected task."""
index = _get_selected_index(todos, evt)
if index is not None:
desc, completed = todos[index]
todos[index] = (desc, not completed)
# Return updated state and updated dataframe display (with selection cleared)
return todos, gr.Dataframe(value=format_todos(todos), selected_index=None)
def delete_task(todos: list[tuple[str, bool]], evt: SelectData):
"""Deletes the selected task."""
index = _get_selected_index(todos, evt)
if index is not None:
del todos[index]
# Return updated state and updated dataframe display (with selection cleared)
return todos, gr.Dataframe(value=format_todos(todos), selected_index=None)