Introducing Gradio Clients

Watch

New to Gradio? Start here: Getting Started

See the Release History

Examples

gradio.Examples(ยทยทยท)

Description

This class is a wrapper over the Dataset component and can be used to create Examples for Blocks / Interfaces. Populates the Dataset component with examples and assigns event listener so that clicking on an example populates the input/output components. Optionally handles example caching for fast inference.

Initialization

Parameters

Attributes

Parameters

Examples

Updating Examples

In this demo, we show how to update the examples by updating the samples of the underlying dataset. Note that this only works if cache_examples=False as updating the underlying dataset does not update the cache.

import gradio as gr

def update_examples(country):
    if country == "USA":
        return gr.Dataset(samples=[["Chicago"], ["Little Rock"], ["San Francisco"]])
    else:
        return gr.Dataset(samples=[["Islamabad"], ["Karachi"], ["Lahore"]])

with gr.Blocks() as demo:
    dropdown = gr.Dropdown(label="Country", choices=["USA", "Pakistan"], value="USA")
    textbox = gr.Textbox()
    examples = gr.Examples([["Chicago"], ["Little Rock"], ["San Francisco"]], textbox)
    dropdown.change(update_examples, dropdown, examples.dataset)
    
demo.launch()

Demos

import gradio as gr def calculator(num1, operation, num2): if operation == "add": return num1 + num2 elif operation == "subtract": return num1 - num2 elif operation == "multiply": return num1 * num2 elif operation == "divide": return num1 / num2 with gr.Blocks() as demo: with gr.Row(): with gr.Column(): num_1 = gr.Number(value=4) operation = gr.Radio(["add", "subtract", "multiply", "divide"]) num_2 = gr.Number(value=0) submit_btn = gr.Button(value="Calculate") with gr.Column(): result = gr.Number() submit_btn.click( calculator, inputs=[num_1, operation, num_2], outputs=[result], api_name=False ) examples = gr.Examples( examples=[ [5, "add", 3], [4, "divide", 2], [-4, "multiply", 2.5], [0, "subtract", 1.2], ], inputs=[num_1, operation, num_2], ) if __name__ == "__main__": demo.launch(show_api=False)

Guides