import panel as pn
pn.extension()
The Toggle
widget allows toggling a single condition between True
/False
states. This widget is interchangeable with the Checkbox
widget.
Discover more on using widgets to add interactivity to your applications in the how-to guides on interactivity. Alternatively, learn how to set up callbacks and (JS-)links between parameters or how to use them as part of declarative UIs with Param.
For details on other options for customizing the component see the layout and styling how-to guides.
value
(boolean): Whether the button is toggled or notbutton_style
(str): The button style, either 'solid' or 'outline'.button_type
(str): A button theme should be one of 'default'
(white), 'primary'
(blue), 'success'
(green), 'info'
(yellow), or 'danger'
(red)icon
(str): An icon to render to the left of the button label. Either an SVG or an icon name which is loaded from tabler-icons.io/.icon_size
(str): Size of the icon as a string, e.g. 12px or 1em.disabled
(boolean): Whether the widget is editablename
(str): The title of the widgettoggle = pn.widgets.Toggle(name='Toggle', button_type='success')
toggle
Toggle.value
is either True or False depending on whether the button is toggled:
toggle.value
The color of the button can be set by selecting one of the available button_type
values and the button_style
can be 'solid'
or 'outline'
:
pn.Row(
*(pn.Column(*(pn.widgets.Toggle(name=p, button_type=p, button_style=bs) for p in pn.widgets.Toggle.param.button_type.objects))
for bs in pn.widgets.Toggle.param.button_style.objects)
)
The Toggle
name string may contain Unicode and Emoji characters, providing a convenient way to define common graphical buttons.
backward = pn.widgets.Toggle(name='\u25c0', width=50)
forward = pn.widgets.Toggle(name='\u25b6', width=50)
search = pn.widgets.Button(name='🔍', width=100)
play = pn.widgets.Toggle(name="▶️ Play", width=100)
pause = pn.widgets.Toggle(name="Pause ⏸️", width=100)
pn.Row(backward, forward, search, play, pause)
However you can also provide an explicit icon
, either as a named icon loaded from tabler-icons.io/:
pn.widgets.Toggle(icon='2fa', button_type='light', icon_size='2em')
or as an explicit SVG:
shuffle_icon = """
<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-arrows-shuffle" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
<path d="M18 4l3 3l-3 3" />
<path d="M18 20l3 -3l-3 -3" />
<path d="M3 7h3a5 5 0 0 1 5 5a5 5 0 0 0 5 5h5" />
<path d="M21 7h-5a4.978 4.978 0 0 0 -3 1m-4 8a4.984 4.984 0 0 1 -3 1h-3" />
</svg>
"""
pn.widgets.Toggle(icon=shuffle_icon, button_type='success', name='Shuffle', icon_size='2em')
The Toggle
widget exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:
pn.Row(toggle.controls(jslink=True), toggle)