gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Separate DropDown's text and value

Open guodage opened this issue 1 year ago • 7 comments

  • [x] I have searched to see if a similar issue already exists.

Is your feature request related to a problem? Please describe.
My case is a DropDown contains a modle file list, which path is really long~~~ eg. a/b/c/d/x.ckpt, so I want to change text to shorter x.ckpt, and maintain the original value.

Describe the solution you'd like
DropDown support a text handle fn, the param is the original value.

Additional context
image

a small question: I've not seen any issue or doc about this, it's my problem?

guodage avatar Jan 19 '23 07:01 guodage

This isn't supported at the moment. However, you can get the index of the selected value (instead of the full value) in the dropdown by setting the type of the Dropdown to be index (e.g. gr.Dropdown(type="index")) and then map the index to anything else. Does that work for you?

abidlabs avatar Jan 19 '23 15:01 abidlabs

I agree this is very nice to have since you won't have to make modifications to the event callback functions just to map between values

space-nuko avatar Jan 25 '23 06:01 space-nuko

Thanks for your opinion, index is a good way in some respects and I alse noted that in the Dropdown’s doc. The reason I didn't use index is my project is StableDiffusion WebUI that based on Gradio, I need change the event logic in the source code, like space-nuko said.

guodage avatar Jan 28 '23 02:01 guodage

I agree this is very nice to have since you won't have to make modifications to the event callback functions just to map between values

Yes, this is my lazy point, hah :) Maybe I could learn to coding a PR for this.

guodage avatar Jan 28 '23 02:01 guodage

I wonder if it would make sense to allow the choices parameter in Dropdown to also accept a list of tuples (text, value), WDYT @aliabid94 @freddyaboulton?

abidlabs avatar Jan 28 '23 06:01 abidlabs

good idea, if the backward compatibility could be taked into consideration.

guodage avatar Feb 02 '23 06:02 guodage

@freddyaboulton @pngwn I'm going to reclassify this as a "new component" since I think this would be a good example of a custom component that has the same frontend as an existing component (Dropdown) but a modified backend / preprocessing logic.

abidlabs avatar Feb 08 '23 00:02 abidlabs

As https://github.com/gradio-app/gradio/issues/5033 was closed in favour of this one, I'd like to suggest expanding this issue to all components with choices (not only dropdown)

And also I'd suggest this to be retrocompatible instead of a new component (an API suggestion to keep both is in #5033)

apolinario avatar Jul 28 '23 14:07 apolinario

Thanks @apolinario, agreed

abidlabs avatar Jul 28 '23 20:07 abidlabs

Todo: we need to fix #3703 as well while doing implementing this

abidlabs avatar Aug 02 '23 22:08 abidlabs

fyi we have a separate issue for the other components that have choices (radio button and checkboxgroup) here: https://github.com/gradio-app/gradio/issues/4754

abidlabs avatar Aug 15 '23 14:08 abidlabs