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

Allow to update the completions options from a parameter #2895

Merged
merged 3 commits into from
Nov 8, 2021
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
6 changes: 5 additions & 1 deletion panel/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,11 @@ def link(change, watchers=[watcher]):
self._rerender()
return
elif change.what == 'objects':
updates['options'] = p_obj.get_range()
options = p_obj.get_range()
if ('options' in widget.param and
isinstance(widget.param['options'], param.List)):
options = list(options)
updates['options'] = options
elif change.what == 'bounds':
start, end = p_obj.get_soft_bounds()
supports_bounds = hasattr(widget, 'start')
Expand Down
30 changes: 23 additions & 7 deletions panel/tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from bokeh.models import (
Div, Slider, Select, RangeSlider as BkRangeSlider, MultiSelect,
Row as BkRow, CheckboxGroup, Toggle, Button, TextInput as
BkTextInput,Tabs as BkTabs, Column as BkColumn, TextInput)
BkTextInput,Tabs as BkTabs, Column as BkColumn, TextInput,
AutocompleteInput as BkAutocompleteInput,
)
from panel.pane import Pane, PaneBase, Matplotlib, Bokeh, HTML
from panel.layout import Tabs, Row
from panel.param import Param, ParamMethod, ParamFunction, JSONInit
Expand Down Expand Up @@ -1235,15 +1237,29 @@ class Test(param.Parameterized):
assert numinput.start == 0
assert numinput.end == 5

def test_set_widget_autocompleteinput():
def test_set_widget_autocompleteinput(document, comm):

class Test(param.Parameterized):
choice = param.Selector(objects=['a', 'b'])
# Testing with default='' and check_on_set=False since this feels
# like the most sensible default config for Selector -> AutocompleteInput
choice = param.Selector(default='', objects=['a', 'b'], check_on_set=False)

test = Test()
p = Param(test, widgets={'choice': AutocompleteInput})
test_pane = Param(test, widgets={'choice': AutocompleteInput})

autocompleteinput = p.layout[1]
model = test_pane.get_root(document, comm=comm)

autocompleteinput = model.children[1]
assert isinstance(autocompleteinput, BkAutocompleteInput)
assert autocompleteinput.completions == ['a', 'b']
assert autocompleteinput.value == ''
assert autocompleteinput.disabled == False

assert autocompleteinput.value == 'a'
assert autocompleteinput.options == ['a', 'b']
# Check changing param value updates widget
test.choice = 'b'
assert autocompleteinput.value == 'b'

# Check changing param attribute updates widget
test.param['choice'].objects = ['c', 'd']
assert autocompleteinput.completions == ['c', 'd']
assert autocompleteinput.value == ''
7 changes: 7 additions & 0 deletions panel/widgets/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ class AutocompleteInput(Widget):

_rename = _AutocompleteInput_rename

def _process_param_change(self, msg):
msg = super()._process_param_change(msg)
if 'completions' in msg:
if self.restrict and not isIn(self.value, msg['completions']):
msg['value'] = self.value = ''
return msg


class _RadioGroupBase(SingleSelectBase):

Expand Down