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

Use non-default Parameterized name as Select label #3755

Merged
merged 1 commit into from
Aug 13, 2022
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
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}$')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like something that should be internal to Param (a method or function to determine if a name is non-default), not hardcoded into Panel.



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