{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import panel as pn\n", "pn.extension()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``MenuButton`` widget allows specifying a list of menu items to select from triggering events when the button is clicked. Unlike other widgets, it does not have a ``value`` parameter. Instead it has a ``clicked`` parameter that can be watched to trigger events and which reports the last clicked menu item.\n", "\n", "For more information about listening to widget events and laying out widgets refer to the [widgets user guide](../../user_guide/Widgets.ipynb). Alternatively you can learn how to build GUIs by declaring parameters independently of any specific widgets in the [param user guide](../../user_guide/Param.ipynb). To express interactivity entirely using Javascript without the need for a Python server take a look at the [links user guide](../../user_guide/Param.ipynb).\n", "\n", "#### Parameters:\n", "\n", "For layout and styling related parameters see the [customization user guide](../../user_guide/Customization.ipynb).\n", "\n", "##### Core\n", "\n", "* **``clicked``** (str): The last clicked menu item\n", "* **``items``** (list(tuple or str or None): Menu items in the dropdown. Allows strings, tuples of the form (title, value) or Nones to separate groups of items.\n", "* **``split``** (boolean): Whether to add separate dropdown area to button.\n", "\n", "##### Display\n", "\n", "* **``button_type``** (str): A button theme; should be one of ``'default'`` (white), ``'primary'`` (blue), ``'success'`` (green), ``'info'`` (yellow), or ``'danger'`` (red)\n", "* **``disabled``** (boolean): Whether the widget is editable\n", "* **``name``** (str): The title of the widget\n", "\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `MenuButton` is defined by the name of the button and a list of items corresponding to the menu items. By separating items by None we can group them into sections:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "menu_items = [('Option A', 'a'), ('Option B', 'b'), ('Option C', 'c'), None, ('Help', 'help')]\n", "\n", "menu_button = pn.widgets.MenuButton(name='Dropdown', items=menu_items, button_type='primary')\n", "\n", "pn.Column(menu_button, height=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``clicked`` parameter will report the last menu item that was clicked:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "menu_button.clicked" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The ``on_click`` method can trigger function when button is clicked:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "text = pn.widgets.TextInput(value='Ready')\n", "\n", "def b(event):\n", " text.value = f'Clicked menu item: \"{event.new}\"'\n", " \n", "menu_button.on_click(b)\n", "\n", "pn.Row(menu_button, text, height=300)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The color of the button can be set by selecting one of the available button types:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.Column(*(pn.widgets.MenuButton(name=p, button_type=p, items=menu_items) for p in pn.widgets.MenuButton.param.button_type.objects))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additionally we can move the dropdown indicator into a separate area using the split option:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.Row(pn.widgets.MenuButton(name='Split Menu', split=True, items=menu_items), height=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If the button itself is clicked in `split` mode, the `clicked` property will report the value of the `name` parameter, i.e. in this case clicking it will set the `clicked` parameter to `'Split Menu'`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Controls\n", "\n", "The `MenuButton` widget exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pn.Row(menu_button.controls, menu_button)" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }