param icon indicating copy to clipboard operation
param copied to clipboard

Support named and positional arguments on bound Parameterized methods

Open MarcSkovMadsen opened this issue 3 years ago • 5 comments

If you have a standalone function you can do

@pn.depends(value1=object1.param.value, value2=object2.param.value)
def view(value1, value2):
    ...

But if its a class method you cannot have named or positional arguments.

import panel as pn
import param
pn.extension()

class Custom(param.Parameterized):
    value1 = param.String()
    value2 = param.String()

    @param.depends("value1", "value2")
    def view(self, value1, value2):
        return value1+value2

custom=Custom()
pn.Column(custom.view, pn.Param(custom)).servable()

image

TypeError: view() missing 2 required positional arguments: 'value1' and 'value2'

Enabling this would make it easier to

  1. reason about which parameters are being using in the method.
  2. Use the values
  3. Refactor code between functional and class based
  4. write in a reactive style ala react.

I would prefer this change would be implemented down to the param level.

Additional Context

If you added the posibility to provide a set_parameters function you would be able to develop in the React style of Idom and Solara.

import panel as pn
import param
pn.extension()

class Custom(param.Parameterized):
    value1 = param.String()
    value2 = param.String()

    @param.depends(value1="value1", value2="value2", react=True, watch=True)
    def view(self, value1, value2, set_parameters):
        ....
        set_parameters(value1=10)

If you simplified we could have yet another api 😄

@param.react(value1="value1", value2="value2", watch=True)
def view(self, value1, value2, set_parameters):
    set_parameters(value1=10)

MarcSkovMadsen avatar Oct 29 '22 12:10 MarcSkovMadsen

@MarcSkovMadsen could you explain what react and set_parameters is supposed to do?

droumis avatar Jan 16 '23 16:01 droumis

The reactive programming style consist of providing 1) the state 2) a function to update the state.

set_parameters is the function 2) to update the state.

react makes sure the set_parameters function is provided as input

MarcSkovMadsen avatar Jan 17 '23 00:01 MarcSkovMadsen

I like the idea of allowing the keyword specification to tell it to pass the parameters in explicitly but I absolutely don't follow the point of the react thing. Why would we provide set_parameters if you can simply do self.param.update(value1=10)?

philippjfr avatar Jan 17 '23 15:01 philippjfr

So currently I'm a strong -1 on react=True and @param.react() but may be missing something.

philippjfr avatar Jan 17 '23 15:01 philippjfr

The reason why I mention the react api is that there are some supply and demand for react apis. And we spend resources on supporting that via Idom and Reacton. So why does Param/Panel not just provide a built in react api.

Its not currently important to me personally. The named arguments to the function is.

MarcSkovMadsen avatar Jan 17 '23 16:01 MarcSkovMadsen