Skip to content

Commit

Permalink
Use non-default Parameterized name as Select label (#3755)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored Aug 13, 2022
1 parent d06c1df commit 4a9b4d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions panel/tests/widgets/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest

from panel.pane import panel
from panel.widgets import (
CrossSelector, MultiChoice, MultiSelect, Select, ToggleGroup,
)
Expand Down Expand Up @@ -59,6 +60,15 @@ def test_select(document, comm):
assert widget.value == str(opts['A'])


def test_select_parameterized_option_labels():
c1 = panel("Value1", name="V1")
c2 = panel("Value2")
c3 = panel("Value3", name="V3")

select = Select(options=[c1, c2, c3], value=c1)
assert select.labels == ['V1', 'Markdown(str)', 'V3']


def test_select_groups_list_options(document, comm):
groups = dict(a=[1, 2], b=[3])
select = Select(value=groups['a'][0], groups=groups, name='Select')
Expand Down
3 changes: 3 additions & 0 deletions panel/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
bokeh_version = Version(bokeh.__version__)


PARAM_NAME_PATTERN = re.compile('^.*\d{5}$')


def isfile(path: str) -> bool:
"""Safe version of os.path.isfile robust to path length issues on Windows"""
try:
Expand Down
10 changes: 8 additions & 2 deletions panel/widgets/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from ..layout import Column, VSpacer
from ..models import CustomSelect, SingleSelect as _BkSingleSelect
from ..util import indexOf, isIn
from ..util import PARAM_NAME_PATTERN, indexOf, isIn
from .base import CompositeWidget, Widget
from .button import Button, _ButtonBase
from .input import TextAreaInput, TextInput
Expand All @@ -41,7 +41,13 @@ class SelectBase(Widget):

@property
def labels(self):
return [str(o) for o in self.options]
labels = []
for o in self.options:
if isinstance(o, param.Parameterized) and not PARAM_NAME_PATTERN.match(o.name):
labels.append(o.name)
else:
labels.append(str(o))
return labels

@property
def values(self):
Expand Down

0 comments on commit 4a9b4d0

Please sign in to comment.