Hi there,
I have a function “def plot_multiple(*args):” in which a user inputs a tissue type - tissues = {
“blood”: 0,
“bone_cancellous”: 1,
“bone_cortical”: 2,
“brain_gm”: 3,
“brain_wm”: 4,
“fat_inf”: 5,
“fat_not_inf”: 6,
“heart”: 7,
“kidney”: 8,
“lens_cortex”: 9,
“liver”: 10,
“lung_inf”: 11,
“muscle”: 12,
“skin_dry”: 13,
“skin_wet”: 14,
“spleen”: 15,
“tendon”: 16
}
The function plots whichever tissuetypes are input, I want to add a widget such that a user can select multiple tissuetypes and they’ll be plotted rather than them typing them out in the funtion.
Any help is appreciated!
Turns out there isn’t actually a checkboxes widget among the selection widgets, though there are others that will pretty much do this out of the box (with awareness of label vs value), such as TagsInput.
If really keen on checkboxes (which have a boolean value) UI:
from ipywidgets import Checkobox, HBox
# the above, but without "smart" quotes
tissues = {"blood": 0}
# make controls
checkboxes = {k: Checkbox(description=k) for k, v in tissues.items()}
# helper function to map from checked/unchecked value to idx
def selected_tissues() -> list[int]:
return [tissues[k] for k, v in checkboxes.items() if v.value]
# wire up observers
[
v.observe(lambda _: plot_multiple(*selected_tissues()), "value")
for v in checkboxes.values()
]
# show them all
HBox(checkboxes.values())
2 Likes