Gradio logo

New to Gradio? Start here: Getting Started

See the Release History

update

gradio.update(kwargs, ยทยทยท)

Description

Updates component properties. When a function passed into a Gradio Interface or a Blocks events returns a typical value, it updates the value of the output component. But it is also possible to update the properties of an output component (such as the number of lines of a `Textbox` or the visibility of an `Image`) by returning the component's `update()` function, which takes as parameters any of the constructor parameters for that component. This is a shorthand for using the update method on a component. For example, rather than using gr.Number.update(...) you can just use gr.update(...). Note that your editor's autocompletion will suggest proper parameters if you use the update method on the component.

Example Usage

# Blocks Example
import gradio as gr
with gr.Blocks() as demo:
    radio = gr.Radio([1, 2, 4], label="Set the value of the number")
    number = gr.Number(value=2, interactive=True)
    radio.change(fn=lambda value: gr.update(value=value), inputs=radio, outputs=number)
demo.launch()

# Interface example
import gradio as gr
def change_textbox(choice):
  if choice == "short":
      return gr.Textbox.update(lines=2, visible=True)
  elif choice == "long":
      return gr.Textbox.update(lines=8, visible=True)
  else:
      return gr.Textbox.update(visible=False)
gr.Interface(
  change_textbox,
  gr.Radio(
      ["short", "long", "none"], label="What kind of essay would you like to write?"
  ),
  gr.Textbox(lines=2),
  live=True,
).launch()

Initialization

Parameter Description
kwargs

<class 'inspect._empty'>

required

Key-word arguments used to update the component's properties.

Demos

import gradio as gr


def change_textbox(choice):
    if choice == "short":
        return gr.Textbox.update(lines=2, visible=True)
    elif choice == "long":
        return gr.Textbox.update(lines=8, visible=True)
    else:
        return gr.Textbox.update(visible=False)


with gr.Blocks() as demo:
    radio = gr.Radio(
        ["short", "long", "none"], label="What kind of essay would you like to write?"
    )
    text = gr.Textbox(lines=2, interactive=True).style(show_copy_button=True)

    radio.change(fn=change_textbox, inputs=radio, outputs=text)


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