Custom Components GalleryNEW

Explore

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

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

Component | list[Component]

required

the component or list of components corresponding to the examples

outputs

Component | list[Component] | None

default: None

optionally, provide the component or list of components corresponding to the output of the examples. Required if cache_examples is not False.

fn

Callable | None

default: None

optionally, provide the function to run to generate the outputs corresponding to the examples. Required if cache_examples is not False. Also required if run_on_click is True.

cache_examples

bool | Literal['lazy'] | None

default: None

If True, caches examples in the server for fast runtime in examples. If "lazy", then examples are cached after their first use. Can also be set by the GRADIO_CACHE_EXAMPLES environment variable, which takes a case-insensitive value, one of: "true", "lazy", or "false" (for the first two to take effect, fn and outputs should also be provided). In HuggingFace Spaces, this is True (as long as fn and outputs are also provided). The default option otherwise is False.

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 not False.

postprocess

bool

default: True

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

api_name

str | Literal[False]

default: "load_example"

Defines how the event associated with clicking on the examples appears in the API docs. Can be a string or False. If set to a string, the endpoint will be exposed in the API docs with the given name. If False, the endpoint will not be exposed in the API docs and downstream apps (including those that gr.load this app) will not be able to use the example function.

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 not False.

Demos

# This demo needs to be run from the repo folder. # python demo/fake_gan/run.py import random import gradio as gr def fake_gan(): images = [ (random.choice( [ "http://www.marketingtool.online/en/face-generator/img/faces/avatar-1151ce9f4b2043de0d2e3b7826127998.jpg", "http://www.marketingtool.online/en/face-generator/img/faces/avatar-116b5e92936b766b7fdfc242649337f7.jpg", "http://www.marketingtool.online/en/face-generator/img/faces/avatar-1163530ca19b5cebe1b002b8ec67b6fc.jpg", "http://www.marketingtool.online/en/face-generator/img/faces/avatar-1116395d6e6a6581eef8b8038f4c8e55.jpg", "http://www.marketingtool.online/en/face-generator/img/faces/avatar-11319be65db395d0e8e6855d18ddcef0.jpg", ] ), f"label {i}") for i in range(3) ] return images with gr.Blocks() as demo: gallery = gr.Gallery( label="Generated images", show_label=False, elem_id="gallery" , columns=[3], rows=[1], object_fit="contain", height="auto") btn = gr.Button("Generate images", scale=0) btn.click(fake_gan, None, gallery) if __name__ == "__main__": demo.launch()

Guides