From c89ee3f09b5172d422c4161bbfa3835d45b27652 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Fri, 12 Jul 2024 15:20:29 +0200 Subject: [PATCH 1/2] Add value_label parameter to Select-like widgets --- panel/widgets/select.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/panel/widgets/select.py b/panel/widgets/select.py index d3bda7ddd8..3ca78780a6 100644 --- a/panel/widgets/select.py +++ b/panel/widgets/select.py @@ -31,7 +31,9 @@ CustomMultiSelect as _BkMultiSelect, CustomSelect, RadioButtonGroup as _BkRadioButtonGroup, SingleSelect as _BkSingleSelect, ) -from ..util import PARAM_NAME_PATTERN, indexOf, isIn +from ..util import ( + PARAM_NAME_PATTERN, edit_readonly, indexOf, isIn, +) from ._mixin import TooltipMixin from .base import CompositeWidget, Widget from .button import Button, _ButtonBase @@ -77,6 +79,8 @@ class SingleSelectBase(SelectBase): value = param.Parameter(default=None) + value_label = param.String(readonly=True) + _allows_values: ClassVar[bool] = True _allows_none: ClassVar[bool] = False @@ -91,6 +95,14 @@ 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): + idx = indexOf(self.value, self.values) + if idx is None: + return + with edit_readonly(self): + self.value_label = self.labels[idx] + def _process_param_change(self, msg): msg = super()._process_param_change(msg) labels, values = self.labels, self.values From d20952f9574bfd28556b8139be49c55b6d03c818 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Fri, 12 Jul 2024 16:00:54 +0200 Subject: [PATCH 2/2] Small fixes --- panel/widgets/select.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/panel/widgets/select.py b/panel/widgets/select.py index 3ca78780a6..baed5fa8e3 100644 --- a/panel/widgets/select.py +++ b/panel/widgets/select.py @@ -79,7 +79,7 @@ class SingleSelectBase(SelectBase): value = param.Parameter(default=None) - value_label = param.String(readonly=True) + value_label = param.String(allow_None=True, readonly=True) _allows_values: ClassVar[bool] = True @@ -87,6 +87,10 @@ class SingleSelectBase(SelectBase): _supports_embed: ClassVar[bool] = True + _rename: ClassVar[Mapping[str, str | None]] = { + 'value_label': None, + } + __abstract = True def __init__(self, **params): @@ -97,11 +101,13 @@ def __init__(self, **params): @param.depends('value', watch=True, on_init=True) def _update_value_label(self): - idx = indexOf(self.value, self.values) - if idx is None: - return + try: + idx = indexOf(self.value, self.values) + label = self.labels[idx] + except ValueError: + label = None with edit_readonly(self): - self.value_label = self.labels[idx] + self.value_label = label def _process_param_change(self, msg): msg = super()._process_param_change(msg)