Skip to content

Commit

Permalink
Added customized Pane type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Nov 14, 2019
1 parent a75cb00 commit 2c14293
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions panel/pane/base.py
Expand Up @@ -69,14 +69,17 @@ class PaneBase(Reactive):
def __init__(self, object=None, **params):
applies = self.applies(object)
if (isinstance(applies, bool) and not applies) and object is not None :
raise ValueError("%s pane does not support objects of type '%s'" %
(type(self).__name__, type(object).__name__))

self._type_error(object)

super(PaneBase, self).__init__(object=object, **params)
kwargs = {k: v for k, v in params.items() if k in Layoutable.param}
self.layout = self.default_layout(self, **kwargs)
self.param.watch(self._update_pane, self._rerender_params)

def _type_error(self, object):
raise ValueError("%s pane does not support objects of type '%s'." %
(type(self).__name__, type(object).__name__))

def __repr__(self, depth=0):
cls = type(self).__name__
params = param_reprs(self, ['object'])
Expand Down
12 changes: 12 additions & 0 deletions panel/pane/image.py
Expand Up @@ -60,6 +60,12 @@ def _is_url(cls, obj):
) and lower_string.endswith('.'+cls.imgtype)
return False

def _type_error(self, object):
if isinstance(object, string_types):
raise ValueError("%s pane cannot parse string that is not a filename "
"or URL." % type(self).__name__)
super(ImageBase, self)._type_error(object)

def _img(self):
if hasattr(self.object, '_repr_{}_'.format(self.imgtype)):
return getattr(self.object, '_repr_' + self.imgtype + '_')()
Expand Down Expand Up @@ -175,6 +181,12 @@ def applies(cls, obj):
return (super(SVG, cls).applies(obj) or
(isinstance(obj, string_types) and obj.lstrip().startswith('<svg')))

def _type_error(self, object):
if isinstance(object, string_types):
raise ValueError("%s pane cannot parse string that is not a filename, "
"URL or a SVG XML contents." % type(self).__name__)
super(SVG, self)._type_error(object)

def _img(self):
if (isinstance(self.object, string_types) and
self.object.lstrip().startswith('<svg')):
Expand Down

0 comments on commit 2c14293

Please sign in to comment.