gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.41k stars 2.52k forks source link

Separate DropDown's text and value #3019

Closed guodage closed 1 year ago

guodage commented 1 year ago

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?

abidlabs commented 1 year ago

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?

space-nuko commented 1 year ago

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

guodage commented 1 year ago

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 commented 1 year ago

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.

abidlabs commented 1 year ago

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?

guodage commented 1 year ago

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

abidlabs commented 1 year ago

@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.

apolinario commented 1 year ago

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)

abidlabs commented 1 year ago

Thanks @apolinario, agreed

abidlabs commented 1 year ago

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

abidlabs commented 1 year ago

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