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

Allow passing Parameter instances to Param pane #303

Merged
merged 2 commits into from
Mar 14, 2019
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
11 changes: 9 additions & 2 deletions panel/param.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class Param(PaneBase):
_rerender_params = []

def __init__(self, object=None, **params):
if isinstance(object, param.Parameter):
if not 'show_name' in params:
params['show_name'] = False
params['parameters'] = [object.name]
object = object.owner
if isinstance(object, param.parameterized.Parameters):
object = object.cls if object.self is None else object.self
if 'parameters' not in params and object is not None:
Expand Down Expand Up @@ -371,11 +376,11 @@ def link(change):
# Set up links to parameterized object
watchers.append(self.object.param.watch(link, p_name, 'constant'))
watchers.append(self.object.param.watch(link, p_name, 'precedence'))
watchers.append(self.object.param.watch(link, p_name))
if hasattr(p_obj, 'get_range'):
watchers.append(self.object.param.watch(link, p_name, 'objects'))
if hasattr(p_obj, 'get_soft_bounds'):
watchers.append(self.object.param.watch(link, p_name, 'bounds'))
watchers.append(self.object.param.watch(link, p_name))

options = kwargs.get('options', [])
if isinstance(options, dict):
Expand Down Expand Up @@ -441,7 +446,9 @@ def _cleanup(self, root):

@classmethod
def applies(cls, obj):
return (is_parameterized(obj) or isinstance(obj, param.parameterized.Parameters))
return (is_parameterized(obj) or
isinstance(obj, param.parameterized.Parameters) or
(isinstance(obj, param.Parameter) and obj.owner is not None))

@classmethod
def widget_type(cls, pobj):
Expand Down
35 changes: 35 additions & 0 deletions panel/tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class Test(param.Parameterized):
assert isinstance(Pane(Test), Param)


def test_instantiate_from_parameter():

class Test(param.Parameterized):

a = param.Number()

assert isinstance(Pane(Test.param.a), Param)


def test_instantiate_from_parameters():

class Test(param.Parameterized):
Expand All @@ -49,6 +58,15 @@ class Test(param.Parameterized):
assert isinstance(Pane(Test()), Param)


def test_instantiate_from_parameter_on_instance():

class Test(param.Parameterized):

a = param.Number()

assert isinstance(Pane(Test().param.a), Param)


def test_instantiate_from_parameters_on_instance():

class Test(param.Parameterized):
Expand Down Expand Up @@ -92,6 +110,23 @@ class Test(param.Parameterized):
assert div.text == '<b>'+test.name[:-5]+'</b>'


def test_single_param(document, comm):

class Test(param.Parameterized):
a = param.Parameter(default=0)

test = Test()
test_pane = Pane(test.param.a)
model = test_pane._get_root(document, comm=comm)

assert isinstance(model, BkColumn)
assert len(model.children) == 1

widget = model.children[0]
assert isinstance(widget, TextInput)
assert widget.value == '0'


def test_get_root_tabs(document, comm):

class Test(param.Parameterized):
Expand Down