{ "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 `DiscreteSlider` widget allows users to select a value from a discrete list or dictionary of values using a slider. Its built upon the [Material UI Slider](https://mui.com/material-ui/react-slider/) component.\n", "\n", "It falls into the broad category of single-value, option-selection widgets that provide a compatible API and include the [`AutocompleteInput`](AutocompleteInput.ipynb), and [`Select`](Select.ipynb) widgets.\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", "* **`disabled`** (`boolean`): Whether the widget is editable.\n", "* **`options`** (`list | dict`): A list or dictionary of options to select from.\n", "* **`value`** (`object`): The current value; must be one of the option values. Updated when the *handle* is dragged. \n", "* **`value_throttled`** (`object`): The current value; must be one of the option values. Updated when the *handle* is released.\n", "\n", "##### Display\n", "\n", "* **`bar_color`** (color): Color of the slider bar as a hexadecimal RGB value.\n", "* **`direction`** (`str`): Whether the slider should go from left to right ('ltr') or right to left ('rtl').\n", "* **`formatter`** (`str`): A custom Python format string. Default is '%.3g'.\n", "* **`label`** (`str`): The title of the widget.\n", "* **`marks`** (`boolean | list[dict]`): Marks indicate predetermined values to which the user can move the slider. If True the `options` are shown as marks. If a list, it should contain dicts with 'value' and an optional 'label' keys.\n", "* **`orientation`** (`Literal[\"horizontal\", \"vertical\"]`): Whether the slider should be displayed in a 'horizontal' or 'vertical' orientation.\n", "* **`show_value`** (`boolean`): Whether to show the widget value as a label or not. \n", "* **`tooltips`** (`boolean | Literal[\"auto\"]`): Whether to display tooltips on the slider handle.\n", "* **`track`** (`Literal[\"normal\", \"inverted\"]`): Whether to display 'normal' or 'inverted'.\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", "- **`name`**: Alias for `label`\n", "\n", "___" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic Usage\n", "\n", "The `DiscreteSlider` allow users to select a value from a discrete range." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider = pmui.DiscreteSlider(label='Age', options=['Ten', 'Twenty', 'Thirty'])\n", "\n", "slider" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Like most other widgets, ``DiscreteSlider`` has a value parameter that can be accessed or set:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "slider.value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `options` parameter also accepts a dictionary whose keys are going to be the labels of the slider. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "value_slider = pmui.DiscreteSlider(label='Age', options={'Ten': 10, 'Twenty': 20, 'Thirty': 30})\n", "\n", "value_slider" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this scenario the `value` will reflect the value of the item in the `options` dictionary:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "value_slider.value" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Label\n", "\n", "You may remove the `label`/ `name` by not setting it." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.DiscreteSlider(options=['Ten', 'Twenty', 'Thirty'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Show Value\n", "\n", "You may remove the value label by setting `show_value=False`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.DiscreteSlider(label='Age', options=['Ten', 'Twenty', 'Thirty'], value='Twenty', show_value=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Disabled\n", "\n", "The widget can be disabled with `disabled=True`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.DiscreteSlider(label='Age', options=['Ten', 'Twenty', 'Thirty'], disabled=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Color\n", "\n", "You can specify a `color`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.DiscreteSlider(label=\"Age\", options=['Ten', 'Twenty', 'Thirty'], color=\"secondary\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Sizes\n", "\n", "For smaller slider, use the parameter `size=\"small\"`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.DiscreteSlider(label='Age Slider', options=['Ten', 'Twenty', 'Thirty'], size=\"small\"),\n", " pmui.DiscreteSlider(label='Age Slider', options=['Ten', 'Twenty', 'Thirty'], size=\"medium\"),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom Marks\n", "\n", "You can have custom marks by providing a rich list to the `marks` parameter. Note that unlike continuous sliders the `value` of the marks should be the integer index of the option." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "marks = [\n", " {\n", " \"value\": 0,\n", " \"label\": '0°C',\n", " },\n", " {\n", " \"value\": 1,\n", " \"label\": '20°C',\n", " },\n", " {\n", " \"value\": 2,\n", " \"label\": '37°C',\n", " },\n", " {\n", " \"value\": 3,\n", " \"label\": '100°C',\n", " },\n", "]\n", "\n", "pmui.DiscreteSlider(options=[0, 20, 37, 100], marks=marks)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Label always visible\n", "\n", "You can force the thumb label to be always visible with `tooltips=True`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.DiscreteSlider(label='Age', options=['Ten', 'Twenty', 'Thirty'], value='Twenty', tooltips=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tracks\n", "\n", "The *track* can be inverted or removed with `track=\"inverted\"` and `track=False` respectively:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.Row(\n", " pmui.DiscreteSlider(label='Age Slider', options=['Ten', 'Twenty', 'Thirty'], value='Twenty', track=False),\n", " pmui.DiscreteSlider(label='Age Slider', options=['Ten', 'Twenty', 'Thirty'], value='Twenty', track=\"inverted\")\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vertical Sliders" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pmui.DiscreteSlider(label='Age Slider', options=['Ten', 'Twenty', 'Thirty'], value='Twenty', orientation=\"vertical\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### API Reference\n", "\n", "The `DiscreteSlider` 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": [ "pmui.DiscreteSlider(options=['Ten', 'Twenty', 'Thirty']).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 Slider:**\n", "\n", "- [Material UI Slider Reference](https://mui.com/material-ui/react-slider/) - Complete documentation for the underlying Material UI component\n", "- [Material UI Slider API](https://mui.com/material-ui/api/slider/) - 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 }