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

Adding the .select event listener #3399

Merged
merged 57 commits into from
Mar 14, 2023
Merged

Adding the .select event listener #3399

merged 57 commits into from
Mar 14, 2023

Conversation

aliabid94
Copy link
Collaborator

@aliabid94 aliabid94 commented Mar 6, 2023

Adds the .select() event listener to various components. This event has EventData, data that is passed along the API to provide additional information about the event trigger. Specifically,

  • Gallery: The EventData for the Gallery component is the index of the gallery item that was clicked.
  • Chatbot: users can select a message
  • CheckboxGroup: refers to user selected option
  • Dataframe: users can select a specific cell
  • Dropdown: refers to user selected option
  • File: users can select a file from a list of files
  • Gallery: users can select an image from a gallery
  • HighlightedText: users can selected a highlighted span
  • Label: users can select a category
  • Radio: refers to user selected option
  • TabItem: refers to own selected tab name
  • Tab: refers to user selected tab name
  • Textbox: Users can select a subset of text

Fixes: #1976

@gradio-pr-bot
Copy link
Collaborator

gradio-pr-bot commented Mar 6, 2023

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

@gradio-pr-bot
Copy link
Collaborator

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

@abidlabs
Copy link
Member

abidlabs commented Mar 7, 2023

A suggestion on naming: how about select() instead of focus()? For two reasons:

  • For js users, I'm pretty sure that focus() is the opposite of blur() and so .focus() would naturally mean an event that gets fired when a component (like a Textbox) is focused on, the opposite of our .blur()
  • In my mind, .select() is more intuitive for this particular action because you are "selecting" one of the images. Similarly, we might "select" one of the Chatbot messages or select one of the choices in a Dropdown or select one of named entities in a HighlightedText

CHANGELOG.md Outdated
@@ -2,7 +2,7 @@


## New Features:
No changes to highlight.
- Added the `.focus()` event listener, applicable right now only to the Gallery component. This event has EventData, data that is passed along the API to provide additional information about the event trigger. The EventData for the Gallery component is the index of the gallery item that was clicked. By [@aliabid94](https://github.com/aliabid94) in [PR 3399](https://github.com/gradio-app/gradio/pull/3399)
Copy link
Member

Choose a reason for hiding this comment

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

Would be great to include a code snippet showing usage in the changelog since this is a pretty big new feature

@abidlabs
Copy link
Member

abidlabs commented Mar 7, 2023

Very cool @aliabid94! Tested and works well. Should get the frontend reviewed as well though

My little demo:

import gradio as gr
import numpy as np

def display(images, event_data: gr.EventData):
    return images[event_data.data["index"]].name

with gr.Blocks() as demo:
    with gr.Row():
        f = gr.Files(file_types=["image"], file_count="multiple")
        g = gr.Gallery().style(preview=True)
        i = gr.Image()
    with gr.Row():
        s = gr.Button("Start")
        
    s.click(lambda files: [i.name for i in files], f, g)
    g.focus(display, f, i)


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

@freddyaboulton
Copy link
Collaborator

freddyaboulton commented Mar 7, 2023

@aliabid94 this looks great to me!

I opened PR #3403 to this PR (lol) to show how we can add type hints to the Gallery Event data. I think that will be better for developer experience.

Type hints in action for @abidlabs 's example:

image

Let me know what you think

@aliabid94 aliabid94 requested a review from pngwn March 7, 2023 17:55
@aliabid94
Copy link
Collaborator Author

A suggestion on naming: how about select() instead of focus()? For two reasons:

The reason I didn't want to choose select is because it seems to be very applicable to Radio, Dropdown, etc., and they already have a change method. We could add select to all of those too I suppose.

I opened PR #3403 to this PR (lol) to show how we can add type hints to the Gallery Event data. I think that will be better for developer experience.

Amazing, thanks @freddyaboulton!

@abidlabs
Copy link
Member

abidlabs commented Mar 7, 2023

I think we should actually add a .select() to those components @aliabid94 (doesn't have to be part of this PR). There's a difference between change() and .select(), which is that .change() is triggered even when the value of the Radio, etc. is changed programmatically whereas select() isn't.

This comes up from time to time, see this issue for a mock example: #1431

@pngwn
Copy link
Member

pngwn commented Mar 7, 2023

@aliabid94 Needs rebasing on main before i can review.

@aliabid94
Copy link
Collaborator Author

Tab: Not working for me. Also I'm confused why you make a distinction between Tab and TabItem -- I thought they were aliases. Here's the code I tried:

Ok fixed. Initially I had only attached the .select to the parent tabs (see blocks_kitchen_sink), but you can now attach it directly to a child Tab as well

I'm thinking ideally in the docs for the .select() component of each component

Done.

gradio/helpers.py Outdated Show resolved Hide resolved
@abidlabs
Copy link
Member

LGTM @aliabid94! Overall docs look great, just left a few nits above for your consideration

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
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.

Very cool PR @aliabid94 ! 🥳

Went through the demos on the PR and everything is working as expected. Didn't detect any regressions in the deployed demos either!

gradio/helpers.py Show resolved Hide resolved
gradio/helpers.py Show resolved Hide resolved
@aliabid94
Copy link
Collaborator Author

We should also document the target and data parameters

documented target, actually hid data for now because I want users to use the subclasses attributes directly. Data is just an ugly json they shouldn't worry about.

@abidlabs
Copy link
Member

LGTM @aliabid94!

@abidlabs
Copy link
Member

I think we're ready to go!! 🚀

@aliabid94 aliabid94 merged commit 95bf08b into main Mar 14, 2023
@aliabid94 aliabid94 deleted the gallery_select branch March 14, 2023 00:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to determine the selected image in a Gallery
5 participants