File size: 2,149 Bytes
9c4abfe
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: blocks_update"]}, {"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", "    gr.Markdown(\n", "        \"\"\"\n", "    # Animal Generator\n", "    Once you select a species, the detail panel should be visible.\n", "    \"\"\"\n", "    )\n", "\n", "    species = gr.Radio(label=\"Animal Class\", choices=[\"Mammal\", \"Fish\", \"Bird\"])\n", "    animal = gr.Dropdown(label=\"Animal\", choices=[])\n", "\n", "    with gr.Column(visible=False) as details_col:\n", "        weight = gr.Slider(0, 20)\n", "        details = gr.Textbox(label=\"Extra Details\")\n", "        generate_btn = gr.Button(\"Generate\")\n", "        output = gr.Textbox(label=\"Output\")\n", "\n", "    species_map = {\n", "        \"Mammal\": [\"Elephant\", \"Giraffe\", \"Hamster\"],\n", "        \"Fish\": [\"Shark\", \"Salmon\", \"Tuna\"],\n", "        \"Bird\": [\"Chicken\", \"Eagle\", \"Hawk\"],\n", "    }\n", "\n", "    def filter_species(species):\n", "        return gr.Dropdown(\n", "            choices=species_map[species], value=species_map[species][1]\n", "        ), gr.Column(visible=True)\n", "\n", "    species.change(filter_species, species, [animal, details_col])\n", "\n", "    def filter_weight(animal):\n", "        if animal in (\"Elephant\", \"Shark\", \"Giraffe\"):\n", "            return gr.Slider(maximum=100)\n", "        else:\n", "            return gr.Slider(maximum=20)\n", "\n", "    animal.change(filter_weight, animal, weight)\n", "    weight.change(lambda w: gr.Textbox(lines=int(w / 10) + 1), weight, details)\n", "\n", "    generate_btn.click(lambda x: x, details, output)\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}