Skip to content

Commit

Permalink
allow to set AutoCompleteInput in a Param pane (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored and philippjfr committed Nov 4, 2021
1 parent 7256c16 commit 3eb6df9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/user_guide/Param.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@
" \"\"\"An example Parameterized class\"\"\"\n",
"\n",
" select_string = param.Selector(objects=[\"red\", \"yellow\", \"green\"])\n",
" autocomplete_string = param.Selector(default='', objects=[\"red\", \"yellow\", \"green\"], check_on_set=False)\n",
" select_number = param.Selector(objects=[0, 1, 10, 100])\n",
"\n",
"\n",
"pn.Param(CustomExample.param, widgets={\n",
" 'select_string': pn.widgets.RadioButtonGroup,\n",
" 'autocomplete_string': pn.widgets.AutocompleteInput,\n",
" 'select_number': pn.widgets.DiscretePlayer}\n",
")"
]
Expand All @@ -218,6 +220,7 @@
"source": [
"pn.Param(CustomExample.param, widgets={\n",
" 'select_string': {'widget_type': pn.widgets.RadioButtonGroup, 'button_type': 'success'},\n",
" 'autocomplete_string': {'widget_type': pn.widgets.AutocompleteInput, 'placeholder': 'Find a color...'},\n",
" 'select_number': pn.widgets.DiscretePlayer}\n",
")"
]
Expand Down
5 changes: 5 additions & 0 deletions panel/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ def widget(self, p_name):
options = p_obj.get_range()
if not options and value is not None:
options = [value]
# This applies to widgets whose `options` Parameter is a List type,
# such as AutoCompleteInput.
if ('options' in widget_class.param
and isinstance(widget_class.param['options'], param.List)):
options = list(options.values())
kw['options'] = options
if hasattr(p_obj, 'get_soft_bounds'):
bounds = p_obj.get_soft_bounds()
Expand Down
15 changes: 14 additions & 1 deletion panel/tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from panel.pane import Pane, PaneBase, Matplotlib, Bokeh, HTML
from panel.layout import Tabs, Row
from panel.param import Param, ParamMethod, ParamFunction, JSONInit
from panel.widgets import LiteralInput, NumberInput, RangeSlider
from panel.widgets import AutocompleteInput, LiteralInput, NumberInput, RangeSlider
from panel.tests.util import mpl_available, mpl_figure


Expand Down Expand Up @@ -1234,3 +1234,16 @@ class Test(param.Parameterized):

assert numinput.start == 0
assert numinput.end == 5

def test_set_widget_autocompleteinput():

class Test(param.Parameterized):
choice = param.Selector(objects=['a', 'b'])

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

autocompleteinput = p.layout[1]

assert autocompleteinput.value == 'a'
assert autocompleteinput.options == ['a', 'b']

0 comments on commit 3eb6df9

Please sign in to comment.