/ipywidgets_toggle_buttons

PYPI package with better toggle buttons

Primary LanguagePythonMIT LicenseMIT

ipywidgets_toggle_buttons

GitHub last commit GitHub license<space><space> Documentation Status PyPI PyPI - Python Version

ipywidgets_toggle_buttons is a simple python package(py>=3.6) with more toggle buttons for ipywidgets

pip install ipywidgets_toggle_buttons

This python package consists of other ToggleButtons classes

  • ToggleButtonsAutoSize - To adjust buttons size to show their's full description automatically
  • MultiToggleButtons - To select a few options at once with usual ToggleButtons interface
  • ToggleButtonsWithHide - ToggleButtonsAutoSize + Hidden options
  • MultiToggleButtonsWithHide - MultiToggleButtons + Hidden options
All of them will adjust size of buttons if options are modified.
Also they have usual interfaces to work with values and options
from ipywidgets_toggle_buttons import ToggleButtonsAutoSize
wid = ToggleButtonsAutoSize(options=[str(i) for i in range(10)])
wid

images/toggle_buttons_auto_size_1.JPG

print(wid.value)  # "0"
print(wid.options)  # ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
print(wid.layout.width)  # '100%'
wid.options = list(wid.options) + ["ajhfkaghnkandjgnakdn"]

images/toggle_buttons_auto_size_2.JPG

from ipywidgets_toggle_buttons import MultiToggleButtons
wid = MultiToggleButtons(
    options=[str(i) for i in range(10)],
    max_chosen_values=2,
)
wid

images/multi_toggle_buttons_1.JPG

def on_value_change(_):
    print("pew")

wid.observe(on_value_change, 'value')
print(wid.value)  # ()
wid.value = ["2", "8"]  # "pew"

images/multi_toggle_buttons_2.JPG

from ipywidgets_toggle_buttons import ToggleButtonsWithHide
wid = ToggleButtonsWithHide(
    value="0",
    options_visible=[str(i) for i in range(10)],
    options_hidden=[str(i) for i in range(5, 15)],
)
wid

images/toggle_buttons_with_hide_1.JPG

After pressing the button Show Hidden Options

images/toggle_buttons_with_hide_2.JPG

Select value 12

images/toggle_buttons_with_hide_3.JPG

And hide Hidden options

images/toggle_buttons_with_hide_4.JPG

print(wid.value)  # "12"
wid.options_visible = [str(i) for i in range(2)]
wid.options_hidden = [f"another {i}" for i in range(2)]

images/toggle_buttons_with_hide_5.JPG

from ipywidgets_toggle_buttons import MultiToggleButtonsWithHide
wid = MultiToggleButtonsWithHide(
    options_visible=[str(i) for i in range(10)],
    options_hidden=[str(i) for i in range(5, 15)],
    max_chosen_values=4,
)
wid

images/multi_toggle_buttons_with_hide_1.JPG

After pressing the button Show Hidden Options

images/multi_toggle_buttons_with_hide_2.JPG

Select a few options and hide all Hidden options

images/multi_toggle_buttons_with_hide_3.JPG

print(wid.value)  # ('1', '9', '11', '13')
print(wid.options_hidden)  # ('10', '11', '12', '13', '14')

If at any moment you want to change the options then it can be done like shown below

wid.options_visible = [str(i) for i in range(2)]
wid.options_hidden = [f"another {i}" for i in range(2)]

This project is licensed under the MIT License.