{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import panel as pn\n", "import panel_material_ui as pmui\n", "\n", "pn.extension()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `CheckButtonGroup` widget allows users to select multiple options from a list by toggling the corresponding buttons. It falls into the broad category of multi-option selection widgets that provide a compatible API and include the [`MultiSelect`](MultiSelect.ipynb), [`CrossSelector`](CrossSelector.ipynb) and [`CheckBoxGroup`](CheckButtonGroup.ipynb) widgets that share a compatible API.\n", "\n", "#### Parameters:\n", "\n", "For more details on customization options, see the [customization guides](https://panel-material-ui.holoviz.org/customization/index.html).\n", "\n", "##### Core\n", "\n", "* **`disabled`** (bool): If True, the widget is not interactive.\n", "* **`options`** (list or dict): The available options to choose from. Can be a list of strings or a dictionary mapping labels to values.\n", "* **`value`** (list): The currently selected options.\n", "\n", "##### Display\n", "\n", "* **`color`** (str): The color variant of the buttons, which must be one of `'default'` (white), `'primary'` (blue), `'success'` (green), `'info'` (yellow), `'light'` (light), or `'danger'` (red).\n", "* **`description`** (str | Bokeh Tooltip | pn.widgets.TooltipIcon): A description which is shown when the widget is hovered.\n", "* **`label`** (str): The title of the widget\n", "* **`loading`** (bool): If True, displays a loading spinner over the component.\n", "* **`orientation`** (str, default='horizontal'): Button group orientation, either 'horizontal' or 'vertical'\n", "* **`size`**: (str, default='medium'): The size of the widget. One of \"small\", \"medium\" or \"large\".\n", "\n", "##### Styling\n", "\n", "- **`sx`** (dict): Component level styling API.\n", "- **`theme_config`** (dict): Theming API.\n", "\n", "##### Aliases\n", "\n", "For compatibility with Panel certain parameters are allowed as aliases:\n", "\n", "- **`button_style`**: Alias for `variant`\n", "- **`button_type`**: Alias for `color`\n", "- **`name`**: Alias for `label`\n", "\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Usage\n", "\n", "Create a `CheckButtonGroup` with a list of options. Users can select multiple items by toggling the corresponding buttons:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkbutton_group = pmui.CheckButtonGroup(label='Check Button Group', value=['Apple', 'Pear'], options=['Apple', 'Banana', 'Pear', 'Strawberry'])\n", "\n", "checkbutton_group" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `value` parameter returns a list of the currently selected options:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "checkbutton_group.value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary Options\n", "\n", "You can provide options as a dictionary where keys are the displayed labels and values are the actual option values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict_group = pmui.CheckButtonGroup(\n", " label='Check Button Group', value=['A', 'P'], options={'Apple': 'A', 'Banana': 'B', 'Pear': 'P', 'Strawberry': 'S'},\n", ")\n", "\n", "dict_group" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's observe how the `value` parameter reflects the selected dictionary values (not the labels):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.pane.Str(dict_group.param.value)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Orientation\n", "\n", "Control the layout of buttons using the `orientation` parameter:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Column(\n", " pmui.CheckButtonGroup(label='Horizontal Group', orientation=\"horizontal\", value=['Apple', 'Pear'], options=['Apple', 'Banana', 'Pear', 'Strawberry']),\n", " pmui.CheckButtonGroup(label='Vertical Group', orientation=\"vertical\", value=['Apple', 'Pear'], options=['Apple', 'Banana', 'Pear', 'Strawberry'])\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Size Options\n", "\n", "Adjust the `CheckButtonGroup` size using the `size` parameter:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.FlexBox(\n", " *(pmui.CheckButtonGroup(label=size, size=size, value=['Apple', 'Pear'], options=['Apple', 'Banana', 'Pear', 'Strawberry']) for size in pmui.CheckButtonGroup.param.size.objects)\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Disabled and Loading States\n", "\n", "Like other widgets, the `CheckButtonGroup` can be disabled to prevent interaction and/or show a loading indicator:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.CheckButtonGroup(label='Disabled Group', disabled=True, loading=True, value=['Apple', 'Pear'], options=['Apple', 'Banana', 'Pear', 'Strawberry'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example: Interactive Pizza Order Form\n", "\n", "Let's create a practical example showing how `CheckBoxGroup` can be used in a real application. This pizza ordering interface demonstrates real-time updates based on user selections:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import panel as pn\n", "import panel_material_ui as pmui\n", "\n", "pn.extension()\n", "\n", "toppings = pmui.CheckButtonGroup(\n", " label=\"Select your toppings:\",\n", " options=['Pepperoni', 'Mushrooms', 'Bell Peppers', 'Onions', 'Olives', 'Extra Cheese'],\n", " value=['Pepperoni', 'Onions'],\n", " sizing_mode=\"stretch_width\",\n", ")\n", "\n", "def create_order_summary(toppings):\n", " summary = f\"## ๐Ÿงบ Your Pizza Order\\n\\n\"\n", " summary += f\"โ€ข Toppings: {', '.join(toppings) if toppings else 'None'}\\n\"\n", " \n", " base_price = 12.99\n", " topping_price = len(toppings) * 1.50\n", " total = base_price + topping_price\n", " summary += f\"\\n**Total: ${total:.2f}**\"\n", " \n", " return summary\n", "\n", "order_summary = pn.bind(create_order_summary, toppings=toppings)\n", "\n", "pmui.Column(\n", " \"## ๐Ÿ• Pizza Order Form\",\n", " toppings,\n", " \"---\",\n", " order_summary,\n", " width=800\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### API Reference\n", "\n", "#### Parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.CheckButtonGroup(label='Check Button Group', value=['Apple', 'Pear'], options=['Apple', 'Banana', 'Pear', 'Strawberry']).api(jslink=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### References\n", "\n", "**Panel Documentation:**\n", "\n", "- [How-to guides on interactivity](https://panel.holoviz.org/how_to/interactivity/index.html) - Learn how to add interactivity to your applications using widgets\n", "- [Setting up callbacks and links](https://panel.holoviz.org/how_to/links/index.html) - Connect parameters between components and create reactive interfaces\n", "- [Declarative UIs with Param](https://panel.holoviz.org/how_to/param/index.html) - Build parameter-driven applications\n", "\n", "**Material UI ToggleButton:**\n", "\n", "- [Material UI ToggleButton Reference](https://mui.com/material-ui/react-toggle-button/) - Complete documentation for the underlying Material UI component\n", "- [Material UI ToggleButtonGroup API](https://mui.com/material-ui/api/toggle-button-group/) - Detailed API reference and configuration options" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.9" } }, "nbformat": 4, "nbformat_minor": 4 }