File size: 2,153 Bytes
299e932
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: todo_list"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "with gr.Blocks() as demo:\n", "    \n", "    tasks = gr.State([])\n", "    new_task = gr.Textbox(label=\"Task Name\", autofocus=True)\n", "\n", "    def add_task(tasks, new_task_name):\n", "        return tasks + [{\"name\": new_task_name, \"complete\": False}], \"\"\n", "\n", "    new_task.submit(add_task, [tasks, new_task], [tasks, new_task])\n", "\n", "    @gr.render(inputs=tasks)\n", "    def render_todos(task_list):\n", "        complete = [task for task in task_list if task[\"complete\"]]\n", "        incomplete = [task for task in task_list if not task[\"complete\"]]\n", "        gr.Markdown(f\"### Incomplete Tasks ({len(incomplete)})\")\n", "        for task in incomplete:\n", "            with gr.Row():\n", "                gr.Textbox(task['name'], show_label=False, container=False)\n", "                done_btn = gr.Button(\"Done\", scale=0)\n", "                def mark_done(task=task):\n", "                    task[\"complete\"] = True\n", "                    return task_list\n", "                done_btn.click(mark_done, None, [tasks])\n", "\n", "                delete_btn = gr.Button(\"Delete\", scale=0, variant=\"stop\")\n", "                def delete(task=task):\n", "                    task_list.remove(task)\n", "                    return task_list\n", "                delete_btn.click(delete, None, [tasks])\n", "\n", "        gr.Markdown(f\"### Complete Tasks ({len(complete)})\")\n", "        for task in complete:\n", "            gr.Textbox(task['name'], show_label=False, container=False)\n", "\n", "\n", "\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}