gradio icon indicating copy to clipboard operation
gradio copied to clipboard

Add a `Textbox` variant that is single line and supports enter-on-click

Open abidlabs opened this issue 2 years ago • 5 comments

To make certain kinds of demos easier (chatbot, image generation, etc), it would be great to have a simple Textbox template that only a single line and supports enter-on-click.

See original comment by @altryne here: https://github.com/gradio-app/gradio/issues/2071#issuecomment-1229222821

abidlabs avatar Aug 28 '22 19:08 abidlabs

Are you aware of any workarounds to this in the meantime? I tried using custom Javascript but I think due to shadow DOM issues was not able to reference another element that I want to trigger based on a text change.

ankrgyl avatar Aug 29 '22 00:08 ankrgyl

You should be able to do this by creating a Textbox with lines=1, max_lines=1, and creating a .submit() event trigger. Let me know if that makes sense.

abidlabs avatar Aug 29 '22 00:08 abidlabs

That worked perfectly! Thank you so much.

ankrgyl avatar Aug 29 '22 01:08 ankrgyl

due to shadow DOM issues was not able to reference another element that I want to trigger based on a text change You can do it like so :

document.querySelector('gradio-app').shadowRoot.querySelector('#some_id')?.click();

altryne avatar Aug 29 '22 04:08 altryne

Oh excellent, that's very helpful @altryne. Thank you. I was able to work around this one with @abidlabs' suggestion but I'll keep that in mind for the future.

ankrgyl avatar Aug 29 '22 04:08 ankrgyl

Revisiting this issue, I think the right approach if someone wants this is to use Blocks and to create the .submit() event trigger instead of having a dedicated component. A minimum working example if anyone stumbles across this in the future:

with gr.Blocks() as demo:
  text1 = gr.Textbox()
  text2 = gr.Textbox()
  text1.submit(lambda x:x, text1, text2)
demo.launch()

Unless I've missed anything, this is pretty general and quite easy to get up and going, so will go ahead and close this issue.

abidlabs avatar Sep 29 '22 20:09 abidlabs