Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support event + request data in gr.render triggers #10190

Merged
merged 4 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-baths-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Support event + request data in gr.render triggers
2 changes: 1 addition & 1 deletion demo/render_tests/run.ipynb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: render_tests"]}, {"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": ["from datetime import datetime\n", "\n", "import gradio as gr\n", "\n", "def update_log():\n", " return datetime.now().timestamp()\n", "\n", "def get_target(evt: gr.EventData):\n", " return evt.target\n", "\n", "def get_select_index(evt: gr.SelectData):\n", " return evt.index\n", "\n", "with gr.Blocks() as demo:\n", " gr.Textbox(value=update_log, every=0.2, label=\"Time\")\n", " \n", " slider = gr.Slider(1, 10, step=1)\n", " @gr.render(inputs=[slider])\n", " def show_log(s):\n", " with gr.Row():\n", " for i in range(s):\n", " gr.Textbox(value=update_log, every=0.2, label=f\"Render {i + 1}\")\n", "\n", " with gr.Row():\n", " selected_btn = gr.Textbox(label=\"Selected Button\")\n", " selected_chat = gr.Textbox(label=\"Selected Chat\")\n", " @gr.render(inputs=[slider])\n", " def show_buttons(s):\n", " with gr.Row():\n", " with gr.Column():\n", " for i in range(s):\n", " btn = gr.Button(f\"Button {i + 1}\")\n", " btn.click(get_target, None, selected_btn)\n", " chatbot = gr.Chatbot([[\"Hello\", \"Hi\"], [\"How are you?\", \"I'm good.\"]])\n", " chatbot.select(get_select_index, None, selected_chat)\n", "\n", " @gr.render()\n", " def examples_in_interface():\n", " gr.Interface(lambda x:x, gr.Textbox(label=\"input\"), gr.Textbox(), examples=[[\"test\"]])\n", "\n", " @gr.render()\n", " def examples_in_blocks():\n", " a = gr.Textbox(label=\"little textbox\")\n", " gr.Examples([[\"abc\"], [\"def\"]], [a])\n", "\n", "\n", "if __name__ == '__main__':\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: render_tests"]}, {"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": ["from datetime import datetime\n", "\n", "import gradio as gr\n", "\n", "def update_log():\n", " return datetime.now().timestamp()\n", "\n", "def get_target(evt: gr.EventData):\n", " return evt.target\n", "\n", "def get_select_index(evt: gr.SelectData):\n", " return evt.index\n", "\n", "with gr.Blocks() as demo:\n", " gr.Textbox(value=update_log, every=0.2, label=\"Time\")\n", " \n", " slider = gr.Slider(1, 10, step=1)\n", " @gr.render(inputs=[slider])\n", " def show_log(s):\n", " with gr.Row():\n", " for i in range(s):\n", " gr.Textbox(value=update_log, every=0.2, label=f\"Render {i + 1}\")\n", "\n", " with gr.Row():\n", " selected_btn = gr.Textbox(label=\"Selected Button\")\n", " selected_chat = gr.Textbox(label=\"Selected Chat\")\n", " @gr.render(inputs=[slider])\n", " def show_buttons(s):\n", " with gr.Row():\n", " with gr.Column():\n", " for i in range(s):\n", " btn = gr.Button(f\"Button {i + 1}\")\n", " btn.click(get_target, None, selected_btn)\n", " chatbot = gr.Chatbot([[\"Hello\", \"Hi\"], [\"How are you?\", \"I'm good.\"]])\n", " chatbot.select(get_select_index, None, selected_chat)\n", "\n", " selectable_chat = gr.Chatbot([[\"chat1\", \"chat2\"], [\"chat3\", \"chat4\"]])\n", " \n", " @gr.render(triggers=[selectable_chat.select])\n", " def show_selected_chat(selection: gr.SelectData):\n", " gr.Textbox(label=\"Trigger Index\", value=selection.index)\n", "\n", " @gr.render()\n", " def examples_in_interface():\n", " gr.Interface(lambda x:x, gr.Textbox(label=\"input\"), gr.Textbox(), examples=[[\"test\"]])\n", "\n", " @gr.render()\n", " def examples_in_blocks():\n", " a = gr.Textbox(label=\"little textbox\")\n", " gr.Examples([[\"abc\"], [\"def\"]], [a])\n", "\n", "\n", "if __name__ == '__main__':\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
6 changes: 6 additions & 0 deletions demo/render_tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def show_buttons(s):
chatbot = gr.Chatbot([["Hello", "Hi"], ["How are you?", "I'm good."]])
chatbot.select(get_select_index, None, selected_chat)

selectable_chat = gr.Chatbot([["chat1", "chat2"], ["chat3", "chat4"]])

@gr.render(triggers=[selectable_chat.select])
def show_selected_chat(selection: gr.SelectData):
gr.Textbox(label="Trigger Index", value=selection.index)

@gr.render()
def examples_in_interface():
gr.Interface(lambda x:x, gr.Textbox(label="input"), gr.Textbox(), examples=[["test"]])
Expand Down
8 changes: 6 additions & 2 deletions gradio/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,9 @@ def set_event_trigger(
f"Invalid value for parameter `trigger_mode`: {trigger_mode}. Please choose from: {['once', 'multiple', 'always_last']}"
)

fn_to_analyze = renderable.fn if renderable else fn
_, progress_index, event_data_index = (
special_args(fn) if fn else (None, None, None)
special_args(fn_to_analyze) if fn_to_analyze else (None, None, None)
)

# If api_name is None or empty string, use the function name
Expand Down Expand Up @@ -1573,8 +1574,11 @@ async def call_function(
dict(zip(block_fn.inputs, processed_input, strict=False))
]

fn_to_analyze = (
block_fn.renderable.fn if block_fn.renderable else block_fn.fn
)
processed_input, progress_index, _ = special_args(
block_fn.fn, processed_input, request, event_data
fn_to_analyze, processed_input, request, event_data
)
progress_tracker = (
processed_input[progress_index] if progress_index is not None else None
Expand Down
4 changes: 4 additions & 0 deletions gradio/renderable.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def show_split(text):
for t in new_triggers
]

if new_triggers:
for trigger in new_triggers: # type: ignore
trigger.callback(trigger.__self__) # type: ignore

def wrapper_function(fn):
Renderable(
fn,
Expand Down
6 changes: 6 additions & 0 deletions js/spa/test/render_tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ test("Test event/selection data works in render", async ({ page }) => {
await expect(selected_chat).toHaveValue("[0, 1]");
});

test("Test event/selection data can trigger render", async ({ page }) => {
await page.getByText("chat3").click();
const selected_chat = page.getByLabel("Trigger Index");
await expect(selected_chat).toHaveValue("[1, 0]");
});

test("Test examples work in render", async ({ page }) => {
await page.getByRole("button", { name: "test" }).click();
await expect(page.getByLabel("input", { exact: true })).toHaveValue("test");
Expand Down
Loading