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 set AutoCompleteInput in a Param pane #2874

Merged
merged 1 commit into from
Nov 2, 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
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']