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 2 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
20 changes: 19 additions & 1 deletion panel/widgets/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,12 +79,18 @@ class SingleSelectBase(SelectBase):

value = param.Parameter(default=None)

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

_allows_values: ClassVar[bool] = True

_allows_none: ClassVar[bool] = False

_supports_embed: ClassVar[bool] = True

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

__abstract = True

def __init__(self, **params):
Expand All @@ -91,6 +99,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
Loading