gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Inconsistent behavior between `return gr.update(value=v)` and `return v`

Open Cynwell opened this issue 4 months ago • 0 comments

Describe the bug

I found that the behavior of returning gr.update(value=v) is different from returning v the variable value itself. Returning v will work, but returning gr.update(value=v) wouldn't work when updating gr.State's value. There is only one line of change between the working and non-working code.

Updating gr.State's value with gr.update(value=v) wouldn't work. However, updating gr.Button's value with gr.update(value=v) worked. Is this an expected design?

Here's the working code:

import gradio as gr


with gr.Blocks() as demo:

    visible = gr.State(False)
    btn = gr.Button(value="Click me")
    btn.click(
        fn=lambda x: print("\n(Before) Visible:", x), inputs=visible, outputs=None
    ).then(
        fn=lambda x: (
            not x,
            gr.update(value=f"Visible: {str(not x)}"),
        ),
        inputs=visible,
        outputs=[visible, btn],
    ).then(
        fn=lambda x: print("(After) Visible:", x), inputs=visible, outputs=None
    )


if __name__ == "__main__":
    demo.launch()

Please see the problematic code below.

Have you searched existing issues? 🔎

  • [X] I have searched and found no existing issues

Reproduction

import gradio as gr


with gr.Blocks() as demo:

    visible = gr.State(False)
    btn = gr.Button(value="Click me")
    btn.click(
        fn=lambda x: print("\n(Before) Visible:", x), inputs=visible, outputs=None
    ).then(
        fn=lambda x: (
            gr.update(value=not x),
            gr.update(value=f"Visible: {str(not x)}"),
        ),
        inputs=visible,
        outputs=[visible, btn],
    ).then(
        fn=lambda x: print("(After) Visible:", x), inputs=visible, outputs=None
    )


if __name__ == "__main__":
    demo.launch()

Screenshot

No response

Logs

No response

System Info

gradio==4.44.0

Severity

I can work around it

Cynwell avatar Oct 03 '24 07:10 Cynwell