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

Add option to hide constant parameters #2637

Merged
merged 2 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion panel/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ class Param(PaneBase):
height = param.Integer(default=None, bounds=(0, None), doc="""
Height of widgetbox the parameter widgets are displayed in.""")

hide_constant = param.Boolean(default=False, doc="""
Whether to hide widgets of constant parameters.""")

initializer = param.Callable(default=None, doc="""
User-supplied function that will be called on initialization,
usually to update the default Parameter values of the
Expand Down Expand Up @@ -213,7 +216,8 @@ def __init__(self, object=None, **params):
type(layout).__name__)
self.param.watch(self._update_widgets, [
'object', 'parameters', 'name', 'display_threshold', 'expand_button',
'expand', 'expand_layout', 'widgets', 'show_labels', 'show_name'])
'expand', 'expand_layout', 'widgets', 'show_labels', 'show_name',
'hide_constant'])
self._update_widgets()

def __repr__(self, depth=0):
Expand Down Expand Up @@ -384,6 +388,8 @@ def widget(self, p_name):
else:
label = p_obj.label
kw = dict(disabled=p_obj.constant, name=label)
if self.hide_constant:
kw['visible'] = not p_obj.constant

value = getattr(self.object, p_name)
if value is not None:
Expand Down Expand Up @@ -460,6 +466,8 @@ def link(change, watchers=[watcher]):
widget = self._widgets[p_name]
if change.what == 'constant':
updates['disabled'] = change.new
if self.hide_constant:
updates['visible'] = not change.new
elif change.what == 'precedence':
if change.new is change.old:
return
Expand Down
16 changes: 16 additions & 0 deletions panel/tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,22 @@ class Test(param.Parameterized):
assert test_pane._widgets['a'] in test_pane._widget_box.objects


def test_hide_constant(document, comm):
class Test(param.Parameterized):
a = param.Number(default=1.2, bounds=(0, 5), constant=True)

test = Test()
test_pane = Pane(test, parameters=['a'], hide_constant=True)
model = test_pane.get_root(document, comm=comm)

slider = model.children[1]
assert not slider.visible

test.param.a.constant = False

assert slider.visible


def test_param_label(document, comm):
class Test(param.Parameterized):
a = param.Number(default=1.2, bounds=(0, 5), label='A')
Expand Down