Skip to content

Commit

Permalink
reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen authored and philippjfr committed Nov 15, 2023
1 parent 16de777 commit f6e03c8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
10 changes: 2 additions & 8 deletions panel/pane/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@
from ..util import param_reprs, param_watchers
from ..util.checks import is_dataframe, is_series
from ..viewable import (
Layoutable, ServableMixin, Viewable, Viewer,
Layoutable, ServableMixin, Viewable, Viewer, _get_items_to_inherit,
)

if TYPE_CHECKING:
from bokeh.document import Document
from bokeh.model import Model
from pyviz_comms import Comm

def _clone_should_inherit(self, p, v):
_p = self.param[p]
return v is not _p.default and not _p.readonly and (v is not None or _p.allow_None)

def panel(obj: Any, **kwargs) -> Viewable:
"""
Creates a displayable Panel object given any valid Python object.
Expand Down Expand Up @@ -385,9 +381,7 @@ def clone(self: T, object: Optional[Any] = None, **params) -> T:
-------
Cloned Pane object
"""
inherited = {
p: v for p, v in self.param.values().items() if _clone_should_inherit(self, p, v)
}
inherited = _get_items_to_inherit(self)
params = dict(inherited, **params)
old_object = params.pop('object', None)
if object is None:
Expand Down
7 changes: 7 additions & 0 deletions panel/tests/pane/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def test_pane_clone(pane):
assert ([(k, v) for k, v in sorted(p.param.values().items()) if k not in ('name', '_pane')] ==
[(k, v) for k, v in sorted(clone.param.values().items()) if k not in ('name', '_pane')])

def test_pane_with_non_defaults_clone():
p = Markdown("Hello World", sizing_mode="stretch_width")
clone = p.clone()

assert ([(k, v) for k, v in sorted(p.param.values().items()) if k not in ('name', '_pane')] ==
[(k, v) for k, v in sorted(clone.param.values().items()) if k not in ('name', '_pane')])


@pytest.mark.parametrize('pane', all_panes)
def test_pane_signature(pane):
Expand Down
15 changes: 15 additions & 0 deletions panel/tests/test_viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,18 @@ def __panel__(self):
return 42

panel(Example())

@pytest.mark.parametrize('viewable', all_viewables)
def test_clone(viewable):
v = Viewable()
clone = v.clone()

assert ([(k, v) for k, v in sorted(v.param.values().items()) if k not in ('name')] ==
[(k, v) for k, v in sorted(clone.param.values().items()) if k not in ('name')])

def test_clone_with_non_defaults():
v= Viewable(loading=True)
clone = v.clone()

assert ([(k, v) for k, v in sorted(v.param.values().items()) if k not in ('name')] ==
[(k, v) for k, v in sorted(clone.param.values().items()) if k not in ('name')])
16 changes: 10 additions & 6 deletions panel/viewable.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
from .io.location import Location
from .io.server import StoppableThread

def _should_inherit(self, p, v):
_p = self.param[p]
return v is not _p.default and not _p.readonly and (v is not None or _p.allow_None)

def _get_items_to_inherit(self: param.Parameterized)->Dict:
return {
p: v for p, v in self.param.values().items()
if _should_inherit(self, p, v)
}

class Layoutable(param.Parameterized):
"""
Expand Down Expand Up @@ -672,7 +681,6 @@ def get_root(
state._views[ref] = (root_view, root, doc, comm)
return root


class Viewable(Renderable, Layoutable, ServableMixin):
"""
Viewable is the baseclass all visual components in the panel
Expand Down Expand Up @@ -851,11 +859,7 @@ def clone(self, **params) -> 'Viewable':
-------
Cloned Viewable object
"""
inherited = {
p: v for p, v in self.param.values().items()
if not self.param[p].readonly and v is not self.param[p].default
and not (v is None and not self.param[p].allow_None)
}
inherited = _get_items_to_inherit(self)
return type(self)(**dict(inherited, **params))

def pprint(self) -> None:
Expand Down

0 comments on commit f6e03c8

Please sign in to comment.