I’m trying to create a wrapper widget that extends an existing widget with a checkbox to make it None
. The plan is to use these widgets on params when allow_None
is set to True
.
So far, I’ve managed to create the following CompositeWidget
specifically for TextInput
although ideally I’d prefer to have a factory function, say MakeOptionalWidget(TextInput)
that creates these classes automatically.
In any case, one thing that I can’t figure out is how to get pn.Param
to show the label above my new widget, i.e. the following
import panel as pn
import param
pn.extension()
class TextInputOptional(pn.widgets.CompositeWidget):
value = param.Parameter()
def __init__(self, **params):
super().__init__(**params)
self._textinput_widget = pn.widgets.TextInput(width=240, margin=(0,0))
self._checkbox_widget = pn.widgets.Checkbox(name="None")
pn.bind(self._update_text_input_state, self._checkbox_widget, watch=True)
pn.bind(self._update_value, self._textinput_widget, watch=True)
self._composite[:] = [
self._textinput_widget,
self._checkbox_widget,
]
def _update_text_input_state(self, val):
self._textinput_widget.disabled = val
if val:
self.value = None
def _update_value(self, val):
self.value = val
class MyParameterized(param.Parameterized):
value_not_nullable = param.String()
value_nullable = param.String(allow_None=True)
pn.Param(
MyParameterized(),
widgets=dict(value_nullable=TextInputOptional),
default_layout=pn.Column,
show_name=True,
show_labels=True,
)
generates the following (with annotation showing missing label).
Alternatively, I suspect Noneable widgets already exist somewhere, so perhaps a pointer to a better implementation than above would be very useful too