panel icon indicating copy to clipboard operation
panel copied to clipboard

Enable from_param to return None when precedence is negative

Open sdc50 opened this issue 3 years ago • 1 comments

Panel==0.12.0 Param==0.11.1

I often use the from_param method on panel widgets now to provide some quick styling to widgets, or to use a different widget than the default for a param object. The syntax is much more succinct than what i was doing previously (usually using pn.Param). However, the from_param method does not support param objects that have a negative precedence.

class Demo(param.Parameterized):
    a = param.Number(default=10, bounds=(0, 10), precedence=-1)
    
    
    def panel(self):
        return pn.widgets.Spinner.from_param(self.param.a)
    
d = Demo()
d.panel()

Executing this code produces the flowing error:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-10-c4f0f419585c> in <module>
      7 
      8 d = Demo()
----> 9 d.panel()

<ipython-input-10-c4f0f419585c> in panel(self)
      4 
      5     def panel(self):
----> 6         return pn.widgets.Spinner.from_param(self.param.a)
      7 
      8 d = Demo()

~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/widgets/base.py in from_param(cls, parameter, **params)
     71         from ..param import Param
     72         layout = Param(parameter, widgets={parameter.name: dict(type=cls, **params)})
---> 73         return layout[0]
     74 
     75     def _get_model(self, doc, root=None, parent=None, comm=None):

~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/pane/base.py in __getitem__(self, index)
    131         Allows pane objects to behave like the underlying layout
    132         """
--> 133         return self.layout[index]
    134 
    135     #----------------------------------------------------------------

~/miniconda/envs/myenv/lib/python3.7/site-packages/panel/layout/base.py in __getitem__(self, index)
    160 
    161     def __getitem__(self, index):
--> 162         return self.objects[index]
    163 
    164     def __len__(self):

IndexError: list index out of range

It seems like this could easily be supported by modifying line 73 of panel/widgets/base.py to:

return layout[0] if layout else None

If this were supported we could use the from_param method universally and not have to consider if the param object in question would ever have a negative precedence.

sdc50 avatar Sep 15 '21 14:09 sdc50

Looks like this was fixed in a different way in https://github.com/holoviz/panel/pull/3199

stuart-cls avatar Sep 14 '22 19:09 stuart-cls