-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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 .key_up
event listener to gr.Dropdown()
#7404
Conversation
🪼 branch checks and previews
Install Gradio from this PR pip install https://gradio-builds.s3.amazonaws.com/8d5e6b4de07c0460290f4fd77eef6c76d2eedfc7/gradio-4.18.0-py3-none-any.whl Install Gradio Python Client from this PR pip install "gradio-client @ git+https://github.com/gradio-app/gradio@8d5e6b4de07c0460290f4fd77eef6c76d2eedfc7#subdirectory=client/python" |
🦄 change detectedThis Pull Request includes changes to the following packages.
With the following changelog entry.
Maintainers or the PR author can modify the PR title to modify this entry.
|
.type
event listener to gr.Dropdown()
.key_down
event listener to gr.Dropdown()
.changeset/shaggy-hairs-peel.md
Outdated
"gradio": minor | ||
--- | ||
|
||
fix:Add `.type` event listener to `gr.Dropdown()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix:Add `.type` event listener to `gr.Dropdown()` | |
fix:Add `.key_down` event listener to `gr.Dropdown()` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the changeset didnt get triggered here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird, maybe b/c this wasn't initially targeting main
, now it is
Keep in mind key_down is triggered on any key press, including arrow keys, shift, etc. which do not affect the actual typed value. Also, key_down is triggered before the value of an input field changes (unlike key up, which triggers after the input value changes) |
Yes, I considered excluding those but figured that it would be better to just provide the
Yup but again I think this is fine because we provide both the |
Why not just use the key up method |
Tbh no strong reason, I can change it |
.key_down
event listener to gr.Dropdown()
.key_up
event listener to gr.Dropdown()
Ok I've changed it to use import gradio as gr
def test(value, key_up_data: gr.KeyUpData):
input_val = key_up_data.input_value
return gr.Dropdown(value=input_val, choices=[f"{input_val} {i}" for i in range(10)])
with gr.Blocks() as demo:
d = gr.Dropdown(["abc", "def"], allow_custom_value=True)
t = gr.JSON()
d.key_up(test, d, d, show_progress="hidden")
if __name__ == "__main__":
demo.launch() still doesn't work, but for a different reason. The problem is the issue that @pngwn brought up before which is when a component is used as an output component, it it set to be static and then converted back to interactive, so it loses its state. In this case, it means that the Dropdown choices are hidden and the Dropdown is blurred. This leads to an unusuable UX. There are a few different ways we could fix it, the simplest being that we don't do this interactive/static switch when |
Ok I've made a temporary workaround by not disabling output components when the event is of type @aliabid94 the demo I shared above should work now. Look ok to merge? |
on:keyup={(e) => | ||
dispatch("key_up", { | ||
key: e.key, | ||
input_value: input_text | ||
})} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would just use keypress here, the issue with keyup
is that it won't fire if you hold the key down even though the text will be displayed which isn't very intuitive.
key_up = EventListener( | ||
"key_up", | ||
doc="This listener is triggered when the user presses a key while the {{ component }} is focused.", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think keypress
is a little more intuitive, partly because we might want to change precisely when this even is fired (keypresses have multiple stages) but also because 'press' is a little more generic. key press phases are pretty close to an implementation detail.
I remember users have asked for adding keyboard shortcuts to their apps before, this api could lead to something like: with gr.Blocks() as demo:
def keylistener(evt: KeyPressEvent):
if evt.keyCode == ...
demo.keypress(keylistener) allowing keypress listeners to a whole app |
Sometimes they want the keypress to do some discreet UI thing that isn't really controllable via python atm though. We have had lots of image editor related keyless request which would be almost impossible to implement via a backend function and would lead to a really bad experience anyway. |
.changeset/tasty-spies-spend.md
Outdated
@@ -3,4 +3,4 @@ | |||
"gradio": patch | |||
--- | |||
|
|||
fix:Retain dropdown value if choices have been changed | |||
fix:Retain dropdown value if choices have been changed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
again, not the correct message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, agree that we should be using KeyPress everywhere instead of KeyUp
Thanks for the reviews @aliabid94 and @pngwn. I'll replace with |
Actually @aliabid94 @pngwn the In most of demos though (including the demo described by @apolinario) I think, |
Closes: #7388 cc @apolinario
Note: I had initially called it
.type()
but that conflicts with theDropdown.type
attribute. Also, because typing does not automatically update thevalue
of theDropdown
component, added a newKeyDownData
class to transmit the relevant data.