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

Enable from_param to return None when precedence is negative #2738

Closed
sdc50 opened this issue Sep 15, 2021 · 2 comments
Closed

Enable from_param to return None when precedence is negative #2738

sdc50 opened this issue Sep 15, 2021 · 2 comments
Labels
good first issue Good for newcomers type: enhancement Minor feature or improvement to an existing feature
Milestone

Comments

@sdc50
Copy link
Contributor

sdc50 commented Sep 15, 2021

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.

@MarcSkovMadsen MarcSkovMadsen added the type: enhancement Minor feature or improvement to an existing feature label Nov 8, 2021
@MarcSkovMadsen MarcSkovMadsen modified the milestones: next, Wishlist Nov 8, 2021
@MarcSkovMadsen MarcSkovMadsen added the good first issue Good for newcomers label Nov 8, 2021
@stuart-cls
Copy link

Looks like this was fixed in a different way in #3199

@philippjfr
Copy link
Member

Indeed!

@philippjfr philippjfr modified the milestones: Wishlist, v1.5.0 Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

4 participants