gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.81k stars 2.29k forks source link

click EventData not working properly for Buttons dynamically rendered with gr.render #8690

Open zacharyzhu1213 opened 6 days ago

zacharyzhu1213 commented 6 days ago

Describe the bug

The EventData object has no target object attached when I used it in the click listeners for the Buttons rendered within gr.render. My use case was to dynamically render a list of all conversations as Buttons in a chatbot app where conversations were activated when the corresponding button was clicked. The reason why I have to use EventData in Button click event listeners was to display button values as human-readable text, e.g. conversation title, coupled with underlying backend id as the Button.key attribute that is used to identify the conversation when it was clicked. Maybe I can work around this by getting rid of gr.render and doing show/hide a fixed number of dummy buttons like mentioned in #2066, but it was neither flexible nor optimal.

Have you searched existing issues? πŸ”Ž

Reproduction

import gradio as gr

with gr.Blocks() as demo:
    text = gr.Textbox()
    @gr.render(inputs=[], triggers=[text.change])
    def list_conversations():
        def btn_click(btn_value, evt: gr.EventData):
            gr.Info(f"{btn_value} clicked with evt.target = {evt.target}")
        for i in range(5):
            btn = gr.Button(f"Conversation {i}", key=f"key-{i}")
            btn.click(btn_click, btn, None)

demo.launch()

Screenshot

image

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Darwin
gradio version: 4.36.1
gradio_client version: 1.0.1

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
altair: 5.3.0
fastapi: 0.111.0
ffmpy: 0.3.2
gradio-client==1.0.1 is not installed.
httpx: 0.27.0
huggingface-hub: 0.23.4
importlib-resources: 6.4.0
jinja2: 3.1.4
markupsafe: 2.1.5
matplotlib: 3.9.0
numpy: 1.26.4
orjson: 3.10.5
packaging: 24.1
pandas: 2.2.2
pillow: 10.3.0
pydantic: 2.7.4
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.4.10
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.12.2
urllib3: 2.2.2
uvicorn: 0.30.1
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2024.6.0
httpx: 0.27.0
huggingface-hub: 0.23.4
packaging: 24.1
typing-extensions: 4.12.2
websockets: 11.0.3

Severity

I can work around it

pngwn commented 6 days ago

cc @aliabid94