panel
panel copied to clipboard
Bind and panel Api: Make it easy to use widgets to select sub layouts
Request
bind
or panel
like functionality to dynamically show value of widget, Parameterized or Parameter value.
Motivation
I'm trying to make a showcase for lightning.ai where a Select or RadioBoxGroup widget is used to select which sub component to show.
I would like to
- use the pn.bind api as that is the recommended one.
- not nest functions inside functions as that makes the logic hard to reason about and test.
- write simple and readable code
But I found it takes thinking and wrapper functions to support this very common workflow with the pn.bind api.
Code: As Is
import panel as pn
# I need a dict instead of list due to this regression: https://github.com/holoviz/panel/issues/3688
options = dict(
PYTORCH=pn.Column("Start Training...", name="Pytorch"),
KERAS=pn.Column("Not yet supported", name="Keras"),
)
selection = pn.widgets.RadioBoxGroup(options=options)
def to_value(value):
# This is a wrapper function
return value
sub_component = pn.bind(to_value, selection)
pn.Column(
selection, sub_component
).servable()
Code: To be
If you define bind_to_value
as
import panel as pn
def to_value(value):
return value
def bind_to_value(widget):
return pn.bind(to_value, widget)
the rest of the code becomes as simple and readable as
options = dict(
PYTORCH=pn.Column("Start Training...", name="Pytorch"),
KERAS=pn.Column("Not yet supported", name="Keras"),
)
selection = pn.widgets.RadioBoxGroup(options=options)
pn.Column(
selection, bind_to_value(selection)
).servable()
Additional context
I've tried to consider the other apis. But I don't want to use interact or Parameterized classes here as pn.bind is the text book api to use. With watch you still need to create a wrapper function.
bind_to_value
could also be called show_value
which would make the code more readable. Or alternative pn.panel
could work like bind_to_value
for widgets and Parameters?