ensure maps are correctly shallow cloned when updating state (#8028)

* ensure maps are correctly shallow cloned when updating state

* cleanup

* add changeset

* fix test

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
pngwn 2024-04-15 17:26:15 -04:00 committed by GitHub
parent dbb7373dde
commit 6fafce0670
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 26 additions and 10 deletions

View File

@ -0,0 +1,7 @@
---
"@gradio/app": patch
"@gradio/dataset": patch
"gradio": patch
---
fix:ensure maps are correctly shallow cloned when updating state

View File

@ -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}
{"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}

View File

@ -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)
demo.launch(show_api=False)

View File

@ -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 };

View File

@ -100,7 +100,7 @@
);
}
$: get_component_meta(selected_samples);
$: component_map, get_component_meta(selected_samples);
</script>
<Block