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

Add a gr.ClearButton #4456

Merged
merged 33 commits into from Jun 14, 2023
Merged

Add a gr.ClearButton #4456

merged 33 commits into from Jun 14, 2023

Conversation

abidlabs
Copy link
Member

@abidlabs abidlabs commented Jun 8, 2023

Adds a gr.ClearButton which can be used to clear the value of components in gr.Blocks(). It is also used internally in gr.Interface.

Example:

import gradio as gr

with gr.Blocks() as demo:
    chatbot = gr.Chatbot([("Hello", "How are you?")])
    with gr.Row():
        textbox = gr.Textbox(scale=3, interactive=True)
        gr.ClearButton([textbox, chatbot], scale=1)
    
demo.launch()

Also removes the cleared_value attribute from components, which will be better for community components

Discussion: There was a discrepancy between the behavior of the Clear button for different components. For 4 of our components (Slider, CheckboxGroup, Radio, Dropdown), the Clear button set the value to the original default value set by the app developer. For all of our other components, such as Textbox, Checkbox, Image, it explicitly cleared the value (e.g. setting it to "" for Textbox, unchecked for Checkbox, or no uploaded image for Image even if there was originally a default). We should have a single behavior for all of our components -- I've set it so it always clears the value as this is what I think most people expect when they hit Clear.

Fixes: #1719
Fixes: #4473

@abidlabs abidlabs marked this pull request as draft June 8, 2023 22:36
@gradio-pr-bot
Copy link
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-4456-all-demos

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Jun 13, 2023

🎉 The demo notebooks match the run.py files! 🎉

@abidlabs abidlabs mentioned this pull request Jun 13, 2023
@abidlabs abidlabs marked this pull request as ready for review June 13, 2023 00:27
@abidlabs abidlabs requested a review from aliabd June 13, 2023 00:37
@abidlabs
Copy link
Member Author

Will fix the tests tomorrow, but everything is working locally so opening up for review!

@freddyaboulton
Copy link
Collaborator

When filing #1719, I had this functionality in mind so it's ok to close it out with this PR.

Copy link
Collaborator

@aliabid94 aliabid94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

gradio/buttons.py Show resolved Hide resolved
gradio/buttons.py Show resolved Hide resolved
@freddyaboulton
Copy link
Collaborator

@abidlabs I agree it's best if the Clear button behaves the same for all components but it's behaved this way for so long I wonder if users have come to expect the current behavior? Maybe it's safest to use the current clear button implementation in interface unitl 4.0?

Copy link
Collaborator

@freddyaboulton freddyaboulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code for ClearButton looks great @abidlabs !

.vscode/settings.json Show resolved Hide resolved
gradio/buttons.py Show resolved Hide resolved
@@ -14,7 +14,7 @@ This tutorial will show how to make several kinds of chatbot UIs with Gradio: fi
$demo_chatbot_streaming

**Prerequisite**: We'll be using the `gradio.Blocks` class to build our Chatbot demo.
You can [read the Guide to Blocks first](https://gradio.app/quickstart/#blocks-more-flexibility-and-control) if you are not already familiar with it.
You can [read the Guide to Blocks first](https://gradio.app/quickstart/#blocks-more-flexibility-and-control) if you are not already familiar with it. Also please make sure you are using the **latest version** version of Gradio: `pip install --upgrade gradio`.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This sentence in the guide is outdated now that the clear internal is abstracted away:

Finally, when the `Clear` button is clicked, we assign `None` to the value of the `Chatbot` to clear its entire history.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh good catch

@abidlabs
Copy link
Member Author

abidlabs commented Jun 13, 2023

@abidlabs I agree it's best if the Clear button behaves the same for all components but it's behaved this way for so long I wonder if users have come to expect the current behavior? Maybe it's safest to use the current clear button implementation in interface unitl 4.0?

I can implement that, but we'd need some stuff on the frontend to store the original default value. I actually think it's okay to change behavior here, because if they are looking to reset values, they can always just refresh the page manually.

@abidlabs
Copy link
Member Author

in light of the above, I’ll go ahead and merge. Thanks @freddyaboulton @aliabid94 for the detailed reviews!

@abidlabs abidlabs merged commit 4729457 into main Jun 14, 2023
20 checks passed
@abidlabs abidlabs deleted the clear-button branch June 14, 2023 00:18
@eshaanagarwal
Copy link

Hey has this been released as part of the new release of Gradio ?

@abidlabs
Copy link
Member Author

Not yet, soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants