File size: 1,903 Bytes
8a08f50
1
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: gallery_selections"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio numpy "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import numpy as np\n", "\n", "with gr.Blocks() as demo:\n", "    imgs = gr.State()\n", "    gallery = gr.Gallery(allow_preview=False)\n", "\n", "    def deselect_images():\n", "        return gr.Gallery(selected_index=None)\n", "\n", "    def generate_images():\n", "        images = []\n", "        for _ in range(9):\n", "            image = np.ones((100, 100, 3), dtype=np.uint8) * np.random.randint(\n", "                0, 255, 3\n", "            )  # image is a solid single color\n", "            images.append(image)\n", "        return images, images\n", "\n", "    demo.load(generate_images, None, [gallery, imgs])\n", "\n", "    with gr.Row():\n", "        selected = gr.Number(show_label=False)\n", "        darken_btn = gr.Button(\"Darken selected\")\n", "    deselect_button = gr.Button(\"Deselect\")\n", "\n", "    deselect_button.click(deselect_images, None, gallery)\n", "\n", "    def get_select_index(evt: gr.SelectData):\n", "        return evt.index\n", "\n", "    gallery.select(get_select_index, None, selected)\n", "\n", "    def darken_img(imgs, index):\n", "        index = int(index)\n", "        imgs[index] = np.round(imgs[index] * 0.8).astype(np.uint8)\n", "        return imgs, imgs\n", "\n", "    darken_btn.click(darken_img, [imgs, selected], [imgs, gallery])\n", "\n", "if __name__ == \"__main__\":\n", "    demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}