Skip to content

Button toggle

Overview

Button toggle is based on the Angular Material button toggle component.

Examples

from dataclasses import field

import mesop as me


@me.stateclass
class State:
  selected_values: list[str] = field(
    default_factory=lambda: ["bold", "underline"]
  )


def load(e: me.LoadEvent):
  me.set_theme_mode("system")


@me.page(
  on_load=load,
  security_policy=me.SecurityPolicy(
    allowed_iframe_parents=["https://google.github.io"]
  ),
  path="/button_toggle",
)
def app():
  state = me.state(State)

  with me.box(style=me.Style(margin=me.Margin.all(15))):
    me.button_toggle(
      value=state.selected_values,
      buttons=[
        me.ButtonToggleButton(label="Bold", value="bold"),
        me.ButtonToggleButton(label="Italic", value="italic"),
        me.ButtonToggleButton(label="Underline", value="underline"),
      ],
      multiple=True,
      hide_selection_indicator=False,
      disabled=False,
      on_change=on_change,
      style=me.Style(margin=me.Margin(bottom=20)),
    )

    me.text("Select buttons: " + " ".join(state.selected_values))


def on_change(e: me.ButtonToggleChangeEvent):
  state = me.state(State)
  state.selected_values = e.values

API

button_toggle

This function creates a button toggle.

PARAMETER DESCRIPTION
value

Selected values of the button toggle.

TYPE: list[str] | str DEFAULT: ''

buttons

List of button toggles.

TYPE: Iterable[ButtonToggleButton]

on_change

Event emitted when the group's value changes.

TYPE: Callable[[ButtonToggleChangeEvent], Any] | None DEFAULT: None

multiple

Whether multiple button toggles can be selected.

TYPE: bool DEFAULT: False

disabled

Whether multiple button toggle group is disabled.

TYPE: bool DEFAULT: False

hide_selection_indicator

Whether checkmark indicator for button toggle groups is hidden.

TYPE: bool DEFAULT: False

style

Style for the component.

TYPE: Style | None DEFAULT: None

key

The component key.

TYPE: str | None DEFAULT: None

ButtonToggleButton dataclass

ATTRIBUTE DESCRIPTION
label

Content to show for the button toggle button

TYPE: str | None

value

The value of the button toggle button.

TYPE: str | None

ButtonToggleChangeEvent dataclass

Bases: MesopEvent

Event representing a change in the button toggle component's selected values.

ATTRIBUTE DESCRIPTION
values

The new values of the button toggle component after the change.

TYPE: list[str]

key

key of the component that emitted this event.

TYPE: str

value property

Shortcut for returning a single value.