Best way to create bidirectional link between parameter and widget attributes?

Hey all, I’m looking to create a bidirectional link between arbitrary widget attributes(either value or options) and parameter. This feels as if it should be very simple and straightforward but isn’t.

A unidirectional link is easily established with by instantiating the widget with the parameter in the value field. However, in this case, when you change the value of the widget from the actual widget, the parameter value does not change.

class TestClass(param.Parameterized):
    some_param = param.String(default='test_value')

test_class = TestClass()

widget = pn.widgets.TextInput(value=test_class.param.some_param)

To get around this, I have to create a watcher which runs whenever I change the widget value:

def update_test_class_param(event):
    test_class.some_param = event.new

widget.param.watch(update_test_class_param, 'value')

Am I missing anything? Is there an easier and more intuitive way to create this sort of workflow?

When using value you can do something like this:

widget = pn.widgets.TextInput.from_param(test_class.param.some_param)
2 Likes

Alternatively if you have a lot of params, pn.Param, but I like from_param more because specifying widgets with their kwargs feels clunky in pn.Param, Param — Panel v1.4.4

That’s a great solution, thanks! Must’ve missed that in the docs.
Is it possible to switch out that parameter with another? or would I need to reinstantiate the widget?

Is it possible to switch out that parameter with another? or would I need to reinstantiate the widget?

I don’t know, I would create a new widget to be sure everything is linked up to the new parameter and not the old parameter.

1 Like

switch out that parameter with another

Can you elaborate on what you mean?

I mean replacing the parameter plugged into the from_param() method with another after the widget has been already instantiated while keeping a bidirectional link.

It seems that I’ll have to update the GUI by instantiating a new widget using from_param() again.

In Panel 1.5.0(?), there will be a placeholder pane, which you can use to re-instantiate the new widget.