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 value_label parameter to Select-like widgets #6977

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 26 additions & 6 deletions panel/widgets/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
RadioButtonGroup as _BkRadioButtonGroup, SingleSelect as _BkSingleSelect,
)
from ..util import (
PARAM_NAME_PATTERN, indexOf, isIn, unique_iterator,
PARAM_NAME_PATTERN, edit_readonly, indexOf, isIn, unique_iterator,
)
from ._mixin import TooltipMixin
from .base import CompositeWidget, Widget
Expand Down Expand Up @@ -79,12 +79,22 @@ class SingleSelectBase(SelectBase):

value = param.Parameter(default=None)

value_label = param.String(allow_None=True, constant=True)

_allows_values: ClassVar[bool] = True

_allows_none: ClassVar[bool] = False

_supports_embed: bool = True

_rename: ClassVar[Mapping[str, str | None]] = {
'value_label': None,
}

_source_transforms: ClassVar[Mapping[str, str | None]] = {
'value_label': None
}

__abstract = True

def __init__(self, **params):
Expand All @@ -93,6 +103,16 @@ def __init__(self, **params):
if self.value is None and None not in values and values and not self._allows_none:
self.value = values[0]

@param.depends('value', watch=True, on_init=True)
def _update_value_label(self):
try:
idx = indexOf(self.value, self.values)
label = self.labels[idx]
except ValueError:
label = None
with edit_readonly(self):
self.value_label = label

def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
labels, values = self.labels, self.values
Expand Down Expand Up @@ -204,7 +224,7 @@ class Select(SingleSelectBase):
}

_source_transforms: ClassVar[Mapping[str, str | None]] = {
'size': None, 'groups': None
'size': None, 'groups': None, 'value_label': None
}

_stylesheets: ClassVar[list[str]] = [f'{CDN_DIST}css/select.css']
Expand Down Expand Up @@ -1012,7 +1032,7 @@ class _RadioGroupBase(SingleSelectBase):
'name': None, 'options': 'labels', 'value': 'active'
}

_source_transforms = {'value': "source.labels[value]"}
_source_transforms = {'value': "source.labels[value]", 'value_label': None}

_target_transforms = {'value': "target.labels.indexOf(value)"}

Expand Down Expand Up @@ -1084,7 +1104,7 @@ class RadioButtonGroup(_RadioGroupBase, _ButtonBase, TooltipMixin):
_rename: ClassVar[Mapping[str, str | None]] = {**_RadioGroupBase._rename, **TooltipMixin._rename}

_source_transforms = {
'value': "source.labels[value]", 'button_style': None, 'description': None
'value': "source.labels[value]", 'button_style': None, 'description': None, 'value_label': None
}

_supports_embed: bool = True
Expand Down Expand Up @@ -1127,7 +1147,7 @@ class _CheckGroupBase(SingleSelectBase):

_rename: ClassVar[Mapping[str, str | None]] = {'name': None, 'options': 'labels', 'value': 'active'}

_source_transforms = {'value': "value.map((index) => source.labels[index])"}
_source_transforms = {'value': "value.map((index) => source.labels[index])", 'value_label': None}

_target_transforms = {'value': "value.map((label) => target.labels.indexOf(label))"}

Expand Down Expand Up @@ -1186,7 +1206,7 @@ class CheckButtonGroup(_CheckGroupBase, _ButtonBase, TooltipMixin):

_source_transforms = {
'value': "value.map((index) => source.labels[index])", 'button_style': None,
'description': None
'description': None, 'value_label': None
}

_widget_type: ClassVar[type[Model]] = _BkCheckboxButtonGroup
Expand Down
Loading