{ "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": [ "Buttons allow users to take actions, and make choices, with a single click or tap. In addition to a `value` parameter, which will toggle from `False` to `True` while the click event is being processed, an additional `clicks` parameter is available, that can be watched to subscribe to click events.\n", "\n", "Buttons communicate actions that users can take. They are typically placed throughout your UI, in places like\n", "\n", "- Modal windows\n", "- Forms\n", "- Cards\n", "- Toolbars\n", "\n", "Discover more on using widgets to add interactivity to your applications in the [how-to guides on interactivity](https://panel.holoviz.org/how_to/interactivity/index.html). Alternatively, learn [how to set up callbacks and (JS-)links between parameters](https://panel.holoviz.org/how_to/links/index.html) or [how to use them as part of declarative UIs with Param](https://panel.holoviz.org/how_to/param/index.html).\n", "\n", "#### Parameters:\n", "\n", "For details on other options for customizing the component see the [customization guides](https://panel-material-ui.holoviz.org/customization/index.html).\n", "\n", "##### Core\n", "\n", "* **`clicks`** (int): Number of clicks (can be listened to)\n", "* **`disabled`** (boolean): Whether the button is clickable.\n", "* **`href`** (str): An optional link to navigate to when clicking the button.\n", "* **`target`** (str): Where to open the `href` link. Default is `_self`, i.e. in the current window or tab.\n", "* **`value`** (boolean): Toggles from `False` to `True` while the event is being processed.\n", "\n", "##### Display\n", "\n", "* **`color`** (str): The color variant of the button, which must be one of `'default'` (white), `'primary'` (blue), `'success'` (green), `'info'` (yellow), `'light'` (light), or `'danger'` (red).\n", "* **`disable_elevation`** (boolean): Whether to apply elevation to the `Button` to visually separate it from the background.\n", "* **`description`** (str | Bokeh Tooltip | pn.widgets.TooltipIcon): A description which is shown when the widget is hovered.\n", "* **`end_icon`** (str): An icon to render to the right of the button label. Either an SVG or an icon name which is loaded from [Material UI Icons](https://mui.com/material-ui/material-icons).\n", "* **`icon`** (str): An icon to render to the left of the button label. Either an SVG or an icon name which is loaded from [Material UI Icons](https://mui.com/material-ui/material-icons).\n", "* **`icon_size`** (str): Size of the icon as a string, e.g. 12px or 1em.\n", "* **`label`** (str): The title of the widget.\n", "* **`variant`** (str): The button style, either 'contained', 'outlined', or 'text'.\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", "#### Methods\n", "\n", "- **`on_click`**: Registers a callback to be executed when the `Button` is clicked.\n", "- **`js_on_click`**: Allows registering a Javascript callback to be executed when the `Button` is clicked.\n", "- **`jscallback`**: Allows registering a Javascript callback to be executed when a property changes on the `Button`.\n", "\n", "##### Callback Arguments\n", "\n", "- **`on_click`** (callable): A function taking an `event` argument. Executed when the `Button` is clicked.\n", "- **`js_on_click`** (str): A string containing the Javascript code. Executed run when the `Button` is clicked.\n", "\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Usage\n", "\n", "The Button comes with three *variants*: text (default), contained, and outlined." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(label=\"Text\", variant=\"text\"),\n", " pmui.Button(label=\"Contained\", variant=\"contained\"),\n", " pmui.Button(label=\"Outlined\", variant=\"outlined\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Text Button\n", "\n", "[Text buttons](https://m2.material.io/components/buttons#text-button) are typically used for less-pronounced actions, including those located in dialogs, and in cards. In cards, text buttons help maintain an emphasis on card content." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(label=\"Primary\"),\n", " pmui.Button(label=\"Disabled\", disabled=True),\n", " pmui.Button(label=\"Link\", href=\"#text-button\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Contained button\n", "\n", "[Contained buttons](https://m2.material.io/components/buttons#contained-button) are high-emphasis, distinguished by their use of elevation and fill. They contain actions that are primary to your app." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(label=\"Contained\", variant=\"contained\"),\n", " pmui.Button(label=\"Disabled\", variant=\"contained\", disabled=True),\n", " pmui.Button(label=\"Link\", variant=\"contained\", href=\"#contained-buttons\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Outlined Button\n", "\n", "[Outlined buttons](https://m2.material.io/components/buttons#outlined-button) are medium-emphasis buttons. They contain actions that are important but aren't the primary action in an app.\n", "\n", "Outlined buttons are also a lower emphasis alternative to contained buttons, or a higher emphasis alternative to text buttons." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(label=\"Primary\", variant=\"outlined\"),\n", " pmui.Button(label=\"Disabled\", variant=\"outlined\", disabled=True),\n", " pmui.Button(label=\"Link\", variant=\"outlined\", href=\"#outlined-buttons\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Link Button\n", "\n", "A `Button` can also have an `href` parameter which will cause it to navigate to the provided link on click:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Button(href=\"https://panel.holoviz.org\", label=\"Open Panel docs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You may additionally specify where to open the link via the `target` parameter:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Button(href=\"https://panel.holoviz.org\", label=\"Open Panel docs in new window or tab\", target=\"_blank\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Handling Clicks\n", "\n", "You can `bind` to the `Button` to trigger actions when the `Button` is clicked." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toggle_button = pmui.Button(label=\"Start Spinning\", variant=\"contained\")\n", "indicator = pmui.LoadingSpinner(value=False, size=25)\n", "\n", "def update_indicator(event):\n", " indicator.value = not indicator.value\n", " toggle_button.label=\"Stop Spinning\" if indicator.value else \"Start Spinning\"\n", "\n", "pn.bind(update_indicator, toggle_button, watch=True)\n", "\n", "pmui.Row(toggle_button, indicator)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively you can use the ``on_click`` method to trigger a function when the button is clicked:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "click_button = pmui.Button(label=\"Increment\", align=\"center\")\n", "text = pmui.TextInput(value='Clicked 0 times', disabled=True)\n", "\n", "def handle_click(event):\n", " text.value = 'Clicked {0} times'.format(click_button.clicks)\n", "\n", "click_button.on_click(handle_click)\n", " \n", "pmui.Row(click_button, text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Disabled and Loading\n", "\n", "Like any other widget the `Button` can be `disabled` and / or set to `loading`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Button(label=\"Loading\", disabled=True, loading=True, color=\"primary\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color\n", "\n", "The color of the button can be set by selecting one of the available `color` values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(color=\"secondary\", label=\"Secondary\"),\n", " pmui.Button(color=\"success\", variant=\"contained\", label=\"Success\"),\n", " pmui.Button(color=\"error\", variant=\"outlined\", label=\"Error\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sizes\n", "\n", "For larger or smaller buttons, use the `size` parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Column(\n", " pmui.Row(\n", " pmui.Button(size=\"small\", label=\"Small\", align=\"center\"), \n", " pmui.Button(size=\"medium\", label=\"Medium\", align=\"center\"),\n", " pmui.Button(size=\"large\", label=\"Large\", align=\"center\"),\n", " ),\n", " pmui.Row(\n", " pmui.Button(size=\"small\", label=\"Small\", variant=\"outlined\", align=\"center\"), \n", " pmui.Button(size=\"medium\", label=\"Medium\", variant=\"outlined\", align=\"center\"),\n", " pmui.Button(size=\"large\", label=\"Large\", variant=\"outlined\", align=\"center\"),\n", " ),\n", " pmui.Row(\n", " pmui.Button(size=\"small\", label=\"Small\", variant=\"contained\", align=\"center\"), \n", " pmui.Button(size=\"medium\", label=\"Medium\", variant=\"contained\", align=\"center\"),\n", " pmui.Button(size=\"large\", label=\"Large\", variant=\"contained\", align=\"center\"),\n", " )\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Please note that, contrast to Material UI you have to `align` a pmui button `center` if you so wish." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Buttons with Icons and Label\n", "\n", "Sometimes you might want to have icons for certain buttons to enhance the UX of the application, as we recognize logo's more easily than plain text. For example, if you have a delete button you can label it with a dustbin icon.\n", "\n", "You may provide an icon either as a named icon from [Material Icon](https://fonts.google.com/icons?icon.set=Material+Icons)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(label=\"Delete\", variant=\"outlined\", icon=\"delete_icon\"),\n", " pmui.Button(label=\"Send\", variant=\"contained\", icon=\"send_icon\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "or as an explicit SVG:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "search_icon = \"\"\"\n", "\n", "\"\"\"\n", "\n", "pmui.Row(\n", " pmui.Button(icon=search_icon, icon_size='1em', label='Search', variant=\"outlined\"),\n", " pmui.Button(icon=search_icon, icon_size='2em', label='Search', variant=\"contained\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also provide an end icon:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Button(label=\"Send\", variant=\"contained\", end_icon=\"send_icon\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively you may use *html codes* and *emojis* in your `label`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.Button(label='\\u25c0', variant=\"outlined\", width=50),\n", " pmui.Button(label='\\u25b6', variant=\"outlined\", width=50),\n", " pmui.Button(label='🔍', variant=\"outlined\", width=100),\n", " pmui.Button(label=\"💾 Save\", variant=\"outlined\", width=100),\n", " pmui.Button(label=\"Copy ✂️\", variant=\"outlined\", width=100),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Icon Button\n", "\n", "See [`ButtonIcon`](IconButton.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Aliases\n", "\n", "For backwards 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`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Button(name=\"Alias\", button_style=\"outlined\", button_type=\"success\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example: Random Quotes" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import panel as pn\n", "import panel_material_ui as pmui\n", "import time\n", "import random\n", "\n", "pn.extension()\n", "\n", "submit = pmui.Button(label=\"Update Quote\", variant=\"contained\", color=\"primary\", size=\"small\", icon=\"refresh\",)\n", "\n", "motivational_quotes = [\n", " \"🚀 The best time to plant a tree was 20 years ago. The second best time is now!\",\n", " \"💪 Success is not final, failure is not fatal: it is the courage to continue that counts.\",\n", " \"🌟 The only way to do great work is to love what you do.\",\n", " \"⚡ Innovation distinguishes between a leader and a follower.\",\n", " \"🎯 The future belongs to those who believe in the beauty of their dreams.\",\n", " \"🔥 Don't watch the clock; do what it does. Keep going!\",\n", " \"✨ Your limitation—it's only your imagination.\",\n", "]\n", "\n", "def pick_quote(value):\n", " yield \"Loading Quote...\"\n", " time.sleep(0.5)\n", " yield random.choice(motivational_quotes)\n", "\n", "quote = pn.bind(pick_quote, submit)\n", "\n", "pmui.Column(quote, submit)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example: Random Quotes (Javascript)\n", "\n", "When you click the button an alert will open and display a quote:" ] }, { "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", "javascript_code = \"\"\"\n", "const quotes = [\n", " \"🚀 The best time to plant a tree was 20 years ago. The second best time is now!\",\n", " \"💪 Success is not final, failure is not fatal: it is the courage to continue that counts.\",\n", " \"🌟 The only way to do great work is to love what you do.\",\n", " \"⚡ Innovation distinguishes between a leader and a follower.\",\n", " \"🎯 The future belongs to those who believe in the beauty of their dreams.\",\n", " \"🔥 Don't watch the clock; do what it does. Keep going!\",\n", " \"✨ Your limitation—it's only your imagination.\"\n", "];\n", "const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];\n", "alert(randomQuote);\n", "\"\"\"\n", "\n", "pmui.Button(label=\"Show Quote\", variant=\"contained\", color=\"primary\", size=\"small\", icon=\"refresh\", js_on_click=javascript_code)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### API Reference\n", "\n", "#### Parameters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Button(label=\"Click Me\").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 Button:**\n", "\n", "- [Material UI Button Reference](https://mui.com/material-ui/react-button/) - Complete documentation for the underlying Material UI component\n", "- [Material UI Button API](https://mui.com/material-ui/api/button/) - 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.2" } }, "nbformat": 4, "nbformat_minor": 4 }