Gradio logo

New to Gradio? Start here: Getting Started

See the Release History

Examples

gradio.Examples(examples, inputs, ยทยทยท)

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

Parameter Description
examples

list[Any] | list[list[Any]] | str

required

example inputs that can be clicked to populate specific components. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided but it should be within the directory with the python file running the gradio app. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs.

inputs

IOComponent | list[IOComponent]

required

the component or list of components corresponding to the examples

outputs

IOComponent | list[IOComponent] | None

default: None

optionally, provide the component or list of components corresponding to the output of the examples. Required if `cache` is True.

fn

Callable | None

default: None

optionally, provide the function to run to generate the outputs corresponding to the examples. Required if `cache` is True.

cache_examples

bool

default: False

if True, caches examples for fast runtime. If True, then `fn` and `outputs` need to be provided

examples_per_page

int

default: 10

how many examples to show per page.

label

str | None

default: "Examples"

the label to use for the examples component (by default, "Examples")

elem_id

str | None

default: None

an optional string that is assigned as the id of this component in the HTML DOM.

run_on_click

bool

default: False

if cache_examples is False, clicking on an example does not run the function when an example is clicked. Set this to True to run the function when an example is clicked. Has no effect if cache_examples is True.

preprocess

bool

default: True

if True, preprocesses the example input before running the prediction function and caching the output. Only applies if cache_examples is True.

postprocess

bool

default: True

if True, postprocesses the example output after running the prediction function and before caching. Only applies if cache_examples is True.

api_name

str | None | Literal[False]

default: False

Defines how the event associated with clicking on the examples appears in the API docs. Can be a string, None, or False. If False (default), the endpoint will not be exposed in the api docs. If set to None, the endpoint will be exposed in the api docs as an unnamed endpoint, although this behavior will be changed in Gradio 4.0. If set to a string, the endpoint will be exposed in the api docs with the given name.

batch

bool

default: False

If True, then the function should process a batch of inputs, meaning that it should accept a list of input values for each parameter. Used only if cache_examples is True.

Demos

import gradio as gr
import os


def combine(a, b):
    return a + " " + b


def mirror(x):
    return x


with gr.Blocks() as demo:

    txt = gr.Textbox(label="Input", lines=2)
    txt_2 = gr.Textbox(label="Input 2")
    txt_3 = gr.Textbox(value="", label="Output")
    btn = gr.Button(value="Submit")
    btn.click(combine, inputs=[txt, txt_2], outputs=[txt_3])

    with gr.Row():
        im = gr.Image()
        im_2 = gr.Image()

    btn = gr.Button(value="Mirror Image")
    btn.click(mirror, inputs=[im], outputs=[im_2])

    gr.Markdown("## Text Examples")
    gr.Examples(
        [["hi", "Adam"], ["hello", "Eve"]],
        [txt, txt_2],
        txt_3,
        combine,
        cache_examples=True,
    )
    gr.Markdown("## Image Examples")
    gr.Examples(
        examples=[os.path.join(os.path.dirname(__file__), "lion.jpg")],
        inputs=im,
        outputs=im_2,
        fn=mirror,
        cache_examples=True,
    )

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