Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Interfaces and Components

Choices, Sliders and Dates

Choose the right Gradio selection component: Dropdown, Radio, CheckboxGroup, Checkbox, Slider, ColorPicker and DateTime, with the Python value each one returns.

Selection components limit what a user can enter, which makes your function safer and your UI friendlier. Dropdown, Radio, CheckboxGroup, Checkbox, Slider, ColorPicker and DateTime cover most needs. The key facts are what each component returns to Python, how choices work with labels versus values, and what happens when nothing is chosen. The Gradio code is read-only; stand-ins implement the value rules so you can test them.

One choice from a list

Python
import gradio as gr

gr.Dropdown(["small", "medium", "large"], value="medium", label="Size")
gr.Radio(["red", "green", "blue"], label="Colour")

The Dropdown page shows a closed box with medium selected that opens a list when clicked. The Radio shows three circles in a row. Both return the chosen string. Pick Radio for up to about five options that the user should see at once and Dropdown for longer lists. Add filterable=True (the default) to let people type to search long Dropdowns. If you want None when nothing is selected, do not set value.

Labels versus values

Choices can be (label, value) pairs, so the user sees friendly text while your function gets a stable key:

Python
gr.Dropdown(
    choices=[("Small (S)", "s"), ("Medium (M)", "m"), ("Large (L)", "l")],
    value="m",
)

The user reads "Small (S)", and the function receives "s". Values can be numbers too, and you can set type="index" to receive the position instead. Using values means you can change display text without breaking your function.

Several choices

Python
gr.CheckboxGroup(["python", "sql", "docker"], label="Skills")
gr.Dropdown(["a", "b", "c"], multiselect=True, max_choices=2)

Both return a list of chosen values (an empty list when none). max_choices limits the count. A single gr.Checkbox(label="Agree") returns a bool.

Sliders

Python
gr.Slider(minimum=0, maximum=1, value=0.7, step=0.05, label="Temperature")

A slider shows a track with a handle and a numeric box that lets people type an exact value. It returns a float (or an int when step is an integer and all values are integers). step controls granularity and randomize=True starts at a random position. Sliders are ideal for model hyperparameters such as temperature, top-p or number of steps. Always set a sensible value, because a slider cannot be empty.

Colour and date/time

Python
gr.ColorPicker(label="Brand colour", value="#3366ff")
gr.DateTime(label="Deadline", include_time=True, type="datetime")

ColorPicker returns a string such as "#3366ff" (the format can also be an rgba(...) string depending on the picker state, so parse defensively). DateTime returns, depending on type, a Python datetime, a numeric timestamp or a string. The page shows a text field with a calendar button and understands strings like now or relative values such as now - 7d. Time zones are easy to get wrong: the value is interpreted in the browser's or the specified timezone, so convert to UTC explicitly when you store it.

Quick reference

Component Returns Empty value
Dropdown str (or list if multiselect) None or []
Radio str None unless value set
CheckboxGroup list[str] []
Checkbox bool False
Slider float / int can't be empty
ColorPicker str None
DateTime datetime, float or str None

Pick the component by the answer

One of a few Radio
One of many Dropdown
Several CheckboxGroup
A range Slider

Fewer typed answers means fewer bugs.

1 / 2

Stand-in 1: choices with labels and values

Python Stand-in: (label, value) choices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in 2: sliders and dates

Python Stand-in: slider snapping and datetime parsing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The slider shows why a handle moved to 0.73 reports 0.75: it snaps to the step grid. The dates show the value of storing UTC.

Common mistakes

  • Comparing against the label text when your choices use (label, value) pairs. Compare against the value.
  • Forgetting that CheckboxGroup returns a list, then calling string methods on it.
  • Using a Dropdown with a value that is not in choices. Newer versions raise a warning or error, so keep them consistent.
  • Treating the slider as an integer when step is fractional.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does gr.CheckboxGroup return?

    Several choices come back as a list, empty when nothing is ticked.
  2. 2. You set choices=[("Small (S)", "s")]. What does the function receive when the user picks it?

    The label is what people read; the second tuple item is the value your function gets.
  3. 3. Which component suits a temperature setting between 0 and 1?

    A slider with a step expresses a numeric range and always has a value.
  4. 4. What type does a single gr.Checkbox return?

    A Checkbox is either ticked or not, so it returns True or False.

Summary

  • Use Radio for few options, Dropdown for many, CheckboxGroup or multiselect for several.
  • (label, value) pairs separate what people see from what your function receives.
  • Sliders snap to step and always hold a value.
  • Handle dates in UTC and parse defensively.

Next: Examples, Flagging and API Names