File size: 4,592 Bytes
c3f69c9
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_group"]}, {"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", "def greet(name):\n", "    return \"Hello \" + name + \"!\"\n", "\n", "with gr.Blocks() as demo:\n", "    gr.Markdown(\"### This is a couple of elements without any gr.Group. Form elements naturally group together anyway.\")\n", "    gr.Textbox(\"A\")\n", "    gr.Number(3)\n", "    gr.Button()\n", "    gr.Image()\n", "    gr.Slider()\n", "\n", "    gr.Markdown(\"### This is the same set put in a gr.Group.\")\n", "    with gr.Group():\n", "        gr.Textbox(\"A\")\n", "        gr.Number(3)\n", "        gr.Button()\n", "        gr.Image()\n", "        gr.Slider()\n", "\n", "    gr.Markdown(\"### Now in a Row, no group.\")\n", "    with gr.Row():\n", "        gr.Textbox(\"A\")\n", "        gr.Number(3)\n", "        gr.Button()\n", "        gr.Image()\n", "        gr.Slider()\n", "\n", "    gr.Markdown(\"### Now in a Row in a group.\")\n", "    with gr.Group():\n", "        with gr.Row():\n", "            gr.Textbox(\"A\")\n", "            gr.Number(3)\n", "            gr.Button()\n", "            gr.Image()\n", "            gr.Slider()\n", "\n", "    gr.Markdown(\"### Several rows grouped together.\")\n", "    with gr.Group():\n", "        with gr.Row():\n", "            gr.Textbox(\"A\")\n", "            gr.Number(3)\n", "            gr.Button()\n", "        with gr.Row():\n", "            gr.Image()\n", "            gr.Audio()\n", "\n", "    gr.Markdown(\"### Several columns grouped together. If columns are uneven, there is a gray group background.\")\n", "    with gr.Group():\n", "        with gr.Row():\n", "            with gr.Column():\n", "                name = gr.Textbox(label=\"Name\")\n", "                btn = gr.Button(\"Hello\")\n", "                gr.Dropdown([\"a\", \"b\", \"c\"], interactive=True)\n", "                gr.Number()\n", "                gr.Textbox()\n", "            with gr.Column():\n", "                gr.Image()\n", "                gr.Dropdown([\"a\", \"b\", \"c\"], interactive=True)\n", "                with gr.Row():\n", "                    gr.Number(scale=2)\n", "                    gr.Textbox()\n", "\n", "    gr.Markdown(\"### container=False removes label, padding, and block border, placing elements 'directly' on background.\")\n", "    gr.Radio([1,2,3], container=False)\n", "    gr.Textbox(container=False)\n", "    gr.Image(\"https://picsum.photos/id/237/200/300\", container=False, height=200)\n", "\n", "    gr.Markdown(\"### Textbox, Dropdown, and Number input boxes takes up full space when within a group without a container.\")\n", "\n", "    with gr.Group():\n", "        name = gr.Textbox(label=\"Name\")\n", "        output = gr.Textbox(show_label=False, container=False)\n", "        greet_btn = gr.Button(\"Greet\")\n", "        with gr.Row():\n", "            gr.Dropdown([\"a\", \"b\", \"c\"], interactive=True, container=False)\n", "            gr.Textbox(container=False)\n", "            gr.Number(container=False)\n", "            gr.Image(height=100)\n", "    greet_btn.click(fn=greet, inputs=name, outputs=output, api_name=\"greet\")\n", "\n", "    gr.Markdown(\"### More examples\")\n", "\n", "    with gr.Group():\n", "        gr.Chatbot()\n", "        with gr.Row():\n", "            name = gr.Textbox(label=\"Prompot\", container=False)\n", "            go = gr.Button(\"go\", scale=0)\n", "\n", "    with gr.Column():\n", "        gr.Radio([1,2,3], container=False)\n", "        gr.Slider(0, 20, container=False)\n", "\n", "    with gr.Group():\n", "        with gr.Row():\n", "            gr.Dropdown([\"a\", \"b\", \"c\"], interactive=True, container=False, elem_id=\"here2\")\n", "            gr.Number(container=False)\n", "            gr.Textbox(container=False)\n", "\n", "    with gr.Row():\n", "        with gr.Column():\n", "            gr.Dropdown([\"a\", \"b\", \"c\"], interactive=True, container=False, elem_id=\"here2\")\n", "        with gr.Column():\n", "           gr.Number(container=False)\n", "        with gr.Column():\n", "            gr.Textbox(container=False)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}