Switches, sliders and pickers

Use a switch, slider or picker when someone can choose a value in your Python script’s native interface. For a practice plan, that might be a warm-up, the length of the session, an instrument or a start time.

A music practice plan on iPhone A music practice plan on iPhone
"""Set a start time, length and warm-up for a music practice session."""

from dataclasses import dataclass
from datetime import time

from juno import ui


@dataclass
class State:
    warm_up: bool = True
    minutes: float = 30
    instrument: str = "piano"
    start: time = time(17, 30)


state = State()


def set_warm_up(value):
    state.warm_up = value


def set_minutes(value):
    state.minutes = value


def set_instrument(tag):
    state.instrument = tag


def set_start(value):
    state.start = value


def body(s):
    return ui.VStack(
        ui.Text("Practice plan", font="title2", weight="bold"),
        ui.Picker("Instrument", options=[("piano", "Piano"), ("cello", "Cello")],
                  value=s.instrument, on_change=set_instrument, style="segmented"),
        ui.DatePicker("Start time", value=s.start, on_change=set_start),
        ui.Toggle("Include warm-up", value=s.warm_up, on_change=set_warm_up),
        ui.Text(f"{int(s.minutes)} minutes of {s.instrument}"),
        ui.Slider(s.minutes, minimum=10, maximum=60, step=5,
                  on_change=set_minutes, accessibility_label="Practice length"),
        ui.Text("Start with scales" if s.warm_up else "Start with a piece"),
        spacing=12, alignment="leading", padding=20, width="fill", max_width=520)


if __name__ == "__main__":
    ui.run(body, state, title="Music practice")

A switch or an amount

A Toggle sends True or False to on_change, while a Slider sends a number. Use the slider’s minimum, maximum and step to offer practice lengths in five-minute steps. Show the amount beside the slider so people can read the value they picked.

A named choice or a time

Give Picker pairs such as ("cello", "Cello"): a tag for your code and a label for the person choosing. Selecting Cello sends the tag "cello" to the callback and changes the practice summary. For DatePicker, a Python date, time or timezone-free datetime selects what to edit, and the callback receives the same type.

Parameters

Toggle

Parameter

Default

Meaning

title

Required

Switch label.

value

Required

Current bool from state.

on_change

Required

Function receiving a bool.

Common keywords

key, padding, disabled, accessibility_label, width, height, min_width, min_height, max_width, max_height. See Description and callback rules.

Slider

Parameter

Default

Meaning

value

Required

Current number between minimum and maximum.

on_change

Required

Function receiving a float.

minimum

0.0

Lower bound; defaults to 0.

maximum

1.0

Upper bound; defaults to 1.

step

None

Optional positive increment.

Common keywords

key, padding, disabled, accessibility_label, width, height, min_width, min_height, max_width, max_height. See Description and callback rules.

Picker

Parameter

Default

Meaning

title

Required

Picker label.

options

Required

Nonempty iterable of unique (string_tag, string_label) pairs.

value

Required

Selected tag, which must exist in options.

on_change

Required

Function receiving a string tag.

style

'menu'

menu or segmented; segmented requires 2–6 options.

size

'regular'

small, regular, or large, a control size, not points.

busy

False

Boolean; show an activity indicator and block selection in menu style.

Common keywords

key, padding, disabled, accessibility_label, width, height, min_width, min_height, max_width, max_height. See Description and callback rules.

DatePicker

Parameter

Default

Meaning

title

Required

Row label.

value

Required

Current date, time or naive datetime from state.

on_change

Required

Function receiving a value of the same type as value.

minimum

None

Optional earliest value, of the same type as value.

maximum

None

Optional latest value, of the same type as value.

Common keywords

key, padding, disabled, accessibility_label, width, height, min_width, min_height, max_width, max_height. See Description and callback rules.

Seen in

Run Tasks from Home’s Native UI section and turn on Due Date for a task to try a switch and a date picker together.