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:
|
buttons |
List of button toggles.
TYPE:
|
on_change |
Event emitted when the group's value changes.
TYPE:
|
multiple |
Whether multiple button toggles can be selected.
TYPE:
|
disabled |
Whether multiple button toggle group is disabled.
TYPE:
|
hide_selection_indicator |
Whether checkmark indicator for button toggle groups is hidden.
TYPE:
|
style |
Style for the component.
TYPE:
|
key |
The component key.
TYPE:
|
ButtonToggleButton
dataclass
¶
ATTRIBUTE | DESCRIPTION |
---|---|
label |
Content to show for the button toggle button
TYPE:
|
value |
The value of the button toggle button.
TYPE:
|
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:
|
key |
key of the component that emitted this event.
TYPE:
|
value
property
¶
Shortcut for returning a single value.