Value error when combining ObjectSelector and AutocompleteInput

Hi, I’m using ObjectSelector to suggest inputs to a field on a form so that users will know what tags they’ve used before. I hit a weird error though where when using ObjectSelector to supply the values to an AutocompleteInput if someone provides a value that isn’t one of those objects then I’ll get a ValueError. Would love some suggestions on how to proceed!

Here’s a working example (just adapted from ActionExample):

class AutocompleteExample(param.Parameterized):
    action = param.Action(default=lambda x: x.param.trigger('action'), label='Click here!')
    show_value = param.String('Nothing loaded')
    number = param.ObjectSelector(objects=['fee', 'fi', 'fo', 'fum'])

    @param.depends('action')
    def get_number(self):
        #self.number += 1
        return f'Value: {self.number}'

    def view(self):
        return pn.Column(pn.widgets.Button.from_param(self.param['action'], button_type = 'primary'),
            pn.widgets.AutocompleteInput.from_param(self.param['number'], restrict=False),
                        '**Click the button** to trigger an update in the output.')
    

auto_example = AutocompleteExample()

pn.Row(
    auto_example.view,
    auto_example.get_number,
).servable()

If I run this with any of the objects from number then it’s fine but otherwise I see ValueError: none not in parameter number's list of possible objects, valid options include [fee, fi, fo, fum]

Maybe pass allow_None=True or set a default value for number.

Unfortunately that didn’t seem to resolve it :frowning:

Ah I read your description more carefully now. I think you should just use number = param.String, then pass options to AutoCompleteInput like:

import param
import panel as pn

pn.extension()


class AutocompleteExample(param.Parameterized):
    action = param.Action(
        default=lambda x: x.param.trigger("action"), label="Click here!"
    )
    show_value = param.String("Nothing loaded")
    number = param.String()

    @param.depends("action")
    def get_number(self):
        # self.number += 1
        return f"Value: {self.number}"

    def view(self):
        return pn.Column(
            pn.widgets.Button.from_param(self.param["action"], button_type="primary"),
            pn.widgets.AutocompleteInput.from_param(
                self.param["number"], options=["fee", "fi", "fo", "fum"], restrict=False,
            ),
            "**Click the button** to trigger an update in the output.",
        )


auto_example = AutocompleteExample()

pn.Row(
    auto_example.view,
    auto_example.get_number,
).show()

Ah, perfect! That works really well in this model though weirdly not in the more complex application that I’m actually building, where options are retrieved from a database depending on a selector. (The options in the actual autocompleteinput don’t seem to update with the selector, though I can see that the apparent options input is changed) I’ll keep working at it and see if I can figure out what’s happening

Thanks so much for the help!

2 Likes