diff --git a/.changeset/eight-lights-jog.md b/.changeset/eight-lights-jog.md new file mode 100644 index 0000000000..4cc8c03979 --- /dev/null +++ b/.changeset/eight-lights-jog.md @@ -0,0 +1,7 @@ +--- +"@gradio/app": patch +"@gradio/dataset": patch +"gradio": patch +--- + +fix:ensure maps are correctly shallow cloned when updating state diff --git a/demo/calculator_blocks/run.ipynb b/demo/calculator_blocks/run.ipynb index 85f90aa55b..f0dbd69cc0 100644 --- a/demo/calculator_blocks/run.ipynb +++ b/demo/calculator_blocks/run.ipynb @@ -1 +1 @@ -{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: calculator_blocks"]}, {"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", "\n", "def calculator(num1, operation, num2):\n", " if operation == \"add\":\n", " return num1 + num2\n", " elif operation == \"subtract\":\n", " return num1 - num2\n", " elif operation == \"multiply\":\n", " return num1 * num2\n", " elif operation == \"divide\":\n", " return num1 / num2\n", "\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " with gr.Column():\n", " num_1 = gr.Number(value=4)\n", " operation = gr.Radio([\"add\", \"subtract\", \"multiply\", \"divide\"])\n", " num_2 = gr.Number(value=0)\n", " submit_btn = gr.Button(value=\"Calculate\")\n", " with gr.Column():\n", " result = gr.Number()\n", "\n", " submit_btn.click(calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False)\n", " examples = gr.Examples(examples=[[5, \"add\", 3],\n", " [4, \"divide\", 2],\n", " [-4, \"multiply\", 2.5],\n", " [0, \"subtract\", 1.2]],\n", " inputs=[num_1, operation, num_2])\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch(show_api=False)"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file +{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: calculator_blocks"]}, {"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", "\n", "def calculator(num1, operation, num2):\n", " if operation == \"add\":\n", " return num1 + num2\n", " elif operation == \"subtract\":\n", " return num1 - num2\n", " elif operation == \"multiply\":\n", " return num1 * num2\n", " elif operation == \"divide\":\n", " return num1 / num2\n", "\n", "\n", "with gr.Blocks() as demo:\n", " with gr.Row():\n", " with gr.Column():\n", " num_1 = gr.Number(value=4)\n", " operation = gr.Radio([\"add\", \"subtract\", \"multiply\", \"divide\"])\n", " num_2 = gr.Number(value=0)\n", " submit_btn = gr.Button(value=\"Calculate\")\n", " with gr.Column():\n", " result = gr.Number()\n", "\n", " submit_btn.click(\n", " calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False\n", " )\n", " examples = gr.Examples(\n", " examples=[\n", " [5, \"add\", 3],\n", " [4, \"divide\", 2],\n", " [-4, \"multiply\", 2.5],\n", " [0, \"subtract\", 1.2],\n", " ],\n", " inputs=[num_1, operation, num_2],\n", " )\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch(show_api=False)\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} \ No newline at end of file diff --git a/demo/calculator_blocks/run.py b/demo/calculator_blocks/run.py index 21b47b94bd..ef6e8b352a 100644 --- a/demo/calculator_blocks/run.py +++ b/demo/calculator_blocks/run.py @@ -22,12 +22,18 @@ with gr.Blocks() as demo: with gr.Column(): result = gr.Number() - submit_btn.click(calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False) - examples = gr.Examples(examples=[[5, "add", 3], - [4, "divide", 2], - [-4, "multiply", 2.5], - [0, "subtract", 1.2]], - inputs=[num_1, operation, num_2]) + submit_btn.click( + calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False + ) + examples = gr.Examples( + examples=[ + [5, "add", 3], + [4, "divide", 2], + [-4, "multiply", 2.5], + [0, "subtract", 1.2], + ], + inputs=[num_1, operation, num_2], + ) if __name__ == "__main__": - demo.launch(show_api=False) \ No newline at end of file + demo.launch(show_api=False) diff --git a/js/app/src/init.ts b/js/app/src/init.ts index 61762a4e9a..2c40886566 100644 --- a/js/app/src/init.ts +++ b/js/app/src/init.ts @@ -183,7 +183,10 @@ export function create_components(): { const instance = instance_map[update.id]; if (!instance) continue; let new_value; - if (Array.isArray(update.value)) new_value = [...update.value]; + if (update.value instanceof Map) new_value = new Map(update.value); + else if (update.value instanceof Set) + new_value = new Set(update.value); + else if (Array.isArray(update.value)) new_value = [...update.value]; else if (update.value === null) new_value = null; else if (typeof update.value === "object") new_value = { ...update.value }; diff --git a/js/dataset/Index.svelte b/js/dataset/Index.svelte index b26ea5dcba..e5b2ffb2d7 100644 --- a/js/dataset/Index.svelte +++ b/js/dataset/Index.svelte @@ -100,7 +100,7 @@ ); } - $: get_component_meta(selected_samples); + $: component_map, get_component_meta(selected_samples);