How to change the labels displayed on a panel widget?

How can I edit a panel button so the labels displayed are different from the attribute values?

Here is an example:


import panel as pn
import panel.widgets as pnw

variable_type = ('ptmp', 'sal')
variable_name=('Temperature',
              'Salinity')              

variables  = pnw.Select(name='Variable', value='ptmp', 
                                 options=list(variable_type),
                       )
variables.show()

Screenshot 2021-01-08 at 19.48.51

Because my button is linked to other panel objects, I cannot change the value and options of the widget. I was wondering if there is an easy way to add an extra variable that would edit the “label” displayed.

I tried modifying the labels argument with :

variables  = pnw.Select(name='Variable', value='ptmp', 
                                 options=list(variable_type),
                                labels=list(variable_name),
                       )


But I don’t think it is the way to do it… I got this error:

WARNING:param.Variable: Setting non-parameter attribute labels=['Temperature', 'Salinity'] using a mechanism intended only for parameters
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-7d643d032a20> in <module>
      6               'Salinity')              
      7 
----> 8 variables  = pnw.Select(name='Variable', value='ptmp', 
      9                                  options=list(variable_type),
     10                                 labels=list(variable_name),

[...]
~/opt/anaconda3/envs/analysis_eel_data/lib/python3.9/site-packages/param/parameterized.py in _setup_params(self_, **params)
   1290                 self.param.warning("Setting non-parameter attribute %s=%s using a mechanism intended only for parameters", name, val)
   1291             # i.e. if not desc it's setting an attribute in __dict__, not a Parameter
-> 1292             setattr(self, name, val)
   1293 
   1294     @classmethod

AttributeError: can't set attribute

Is there an easy way to do that?

You can use a dictionary for the options.

1 Like

Thank you. After a few try, I managed to make it work! In my case the solution was:

import panel as pn
import panel.widgets as pnw


variable_name= ('ptmp', 'psal')
variable_label=('Potential Temperature',
              'Salinity',
              )
dict_opt = {label:variable for label, variable in zip(variable_label, variable_name)}

variables  = pnw.Select(name='Variable', value='ptmp', 
                                 options=dict_opt,
                       )
variables.show()

The Parameter docstring for the method .Select() give no information on the options parameter:

options:       < No docstring available >