Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nested select #5791

Merged
merged 19 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
254 changes: 254 additions & 0 deletions examples/reference/widgets/NestedSelect.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import panel as pn\n",
"pn.extension()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Discover more on using widgets to add interactivity to your applications in the [how-to guides on interactivity](../how_to/interactivity/index.md). Alternatively, learn [how to set up callbacks and (JS-)links between parameters](../../how_to/links/index.md) or [how to use them as part of declarative UIs with Param](../../how_to/param/index.html).\n",
"\n",
"#### Parameters:\n",
"\n",
"For details on other options for customizing the component see the [layout](../../how_to/layout/index.md) and [styling](../../how_to/styling/index.md) how-to guides.\n",
"\n",
"##### Core\n",
"\n",
"* **``options``** (dict): The options to select from. The options may be nested dictionaries or lists.\n",
"* **``value``** (dict): The value from all the Select widgets; the keys are the levels names. If no levels names are specified, the keys are the levels indices.\n",
"* **``levels``** (list): Either a list of strings or a list of dictionaries. If a list of strings, the strings are used as the names of the levels. If a list of dictionaries, each dictionary may have a \"name\" key, which is used as the name of the level, a \"type\" key, which is used as the type of widget, and any corresponding widget keyword arguments.\n",
"\n",
"##### Display\n",
"\n",
"* **``disabled``** (boolean): Whether the widget is editable\n",
"* **``name``** (str): The title of the widget\n",
"\n",
"___"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select = pn.widgets.NestedSelect(\n",
" options={\n",
" \"GFS\": {\n",
" \"0.25 deg\": [\"00Z\", \"06Z\", \"12Z\", \"18Z\"],\n",
" \"0.5 deg\": [\"00Z\", \"12Z\"],\n",
" \"1 deg\": [\"00Z\", \"12Z\"],\n",
" },\n",
" \"NAME\": {\n",
" \"12 km\": [\"00Z\", \"12Z\"],\n",
" \"3 km\": [\"00Z\", \"12Z\"],\n",
" },\n",
" },\n",
" levels=[\"Model\", \"Resolution\", \"Initialization\"],\n",
")\n",
"select"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like most other widgets, ``NestedSelect`` has a value parameter that can be accessed or set:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select.value"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If ``levels`` names are not set, the value is keyed off the level index."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select = pn.widgets.NestedSelect(\n",
" options={\n",
" \"GFS\": {\n",
" \"0.25 deg\": [\"00Z\", \"06Z\", \"12Z\", \"18Z\"],\n",
" \"0.5 deg\": [\"00Z\", \"12Z\"],\n",
" \"1 deg\": [\"00Z\", \"12Z\"],\n",
" },\n",
" \"NAME\": {\n",
" \"12 km\": [\"00Z\", \"12Z\"],\n",
" \"3 km\": [\"00Z\", \"12Z\"],\n",
" },\n",
" },\n",
")\n",
"select.value"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also define the default value by providing a dict."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select = pn.widgets.NestedSelect(\n",
" options={\n",
" \"GFS\": {\n",
" \"0.25 deg\": [\"00Z\", \"06Z\", \"12Z\", \"18Z\"],\n",
" \"0.5 deg\": [\"00Z\", \"12Z\"],\n",
" \"1 deg\": [\"00Z\", \"12Z\"],\n",
" },\n",
" \"NAME\": {\n",
" \"12 km\": [\"00Z\", \"12Z\"],\n",
" \"3 km\": [\"00Z\", \"12Z\"],\n",
" },\n",
" },\n",
" value={\"Model\": \"NAME\", \"Resolution\": \"12 km\", \"Initialization\": \"12Z\"},\n",
" levels=[\"Model\", \"Resolution\", \"Initialization\"],\n",
")\n",
"select"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not all keys of the value need to be specified, and the keys can be specified in any order."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select = pn.widgets.NestedSelect(\n",
" options={\n",
" \"GFS\": {\n",
" \"0.25 deg\": [\"00Z\", \"06Z\", \"12Z\", \"18Z\"],\n",
" \"0.5 deg\": [\"00Z\", \"12Z\"],\n",
" \"1 deg\": [\"00Z\", \"12Z\"],\n",
" },\n",
" \"NAME\": {\n",
" \"12 km\": [\"00Z\", \"12Z\"],\n",
" \"3 km\": [\"00Z\", \"12Z\"],\n",
" },\n",
" },\n",
" value={\"Initialization\": \"12Z\", \"Resolution\": \"0.5 deg\"},\n",
" levels=[\"Model\", \"Resolution\", \"Initialization\"],\n",
")\n",
"select"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An incomplete definition of options can also be used. The corresponding subsequent widgets will be hidden."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select = pn.widgets.NestedSelect(\n",
" options={\n",
" \"NAME\": {},\n",
" \"GFS\": {\n",
" \"0.25 deg\": [\"00Z\", \"06Z\", \"12Z\", \"18Z\"],\n",
" \"0.5 deg\": [\"00Z\", \"12Z\"],\n",
" \"1 deg\": [\"00Z\", \"12Z\"],\n",
" },\n",
" },\n",
" levels=[\"Model\", \"Resolution\", \"Initialization\"],\n",
")\n",
"select"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The value for the hidden widgets will be `None`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select.value = {\"Model\": \"NAME\"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`levels` also accepts a list of dicts, where each dict contains the type of widget and its corresponding kwargs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"select = pn.widgets.NestedSelect(\n",
" options={\n",
" \"GFS\": {\n",
" \"0.25 deg\": [\"00Z\", \"06Z\", \"12Z\", \"18Z\"],\n",
" \"0.5 deg\": [\"00Z\", \"12Z\"],\n",
" \"1 deg\": [\"00Z\", \"12Z\"],\n",
" },\n",
" \"NAME\": {\n",
" \"12 km\": [\"00Z\", \"12Z\"],\n",
" \"3 km\": [\"00Z\", \"12Z\"],\n",
" },\n",
" },\n",
" value={\"Model\": \"NAME\", \"Resolution\": \"12 km\", \"Initialization\": \"00Z\"},\n",
" levels=[\n",
" {\"name\": \"Model\", \"type\": pn.widgets.RadioButtonGroup, \"button_type\": \"primary\"},\n",
" {\"name\": \"Resolution\", \"type\": pn.widgets.Select, \"width\": 100},\n",
" {\"name\": \"Initialization\", \"type\": pn.widgets.DiscreteSlider, \"width\": 100},\n",
" ],\n",
")\n",
"select"
]
}
],
"metadata": {
"language_info": {
"name": "python",
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading
Loading