Skip to content

Commit

Permalink
Added HTML repr for Parameterized
Browse files Browse the repository at this point in the history
  • Loading branch information
jbednar committed Aug 16, 2020
1 parent 97a26a7 commit 391e4dc
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -2400,9 +2400,55 @@ def type_script_repr(type_,imports,prefix,settings):
dbprint_prefix=None


def name_if_set(parameterized):
"""Return the name of this Parameterized if explicitly set to other than the default"""
class_name = parameterized.__class__.__name__
default_name = re.match('^'+class_name+'[0-9]+$', parameterized.name)
return '' if default_name else parameterized.name



def _get_param_repr(key, val, p, truncate=40):
"""HTML representation for a single Parameter object and its value"""
if hasattr(val, "_repr_html_"):
try:
value = val._repr_html_(open=False)
except:
value = val._repr_html_()
else:
rep = repr(val)
value = (rep[:truncate] + '..') if len(rep) > truncate else rep

constant = 'C' if p.constant else 'V'
readonly = 'RO' if p.readonly else 'RW'
allow_None = ' AN' if hasattr(p, 'allow_None') and p.allow_None else ''
mode = '%s %s%s' % (constant, readonly, allow_None)

return f' <tr>' \
f'<td><tt>{key}</tt></td>' \
f'<td>{p.__class__.__name__}</td>' \
f'<td>{getattr(p,"bounds","")}</td>' \
f'<td>{mode}</td>' \
f'<td>{value}</td>' \
f'</tr>\n'


def _parameterized_repr_html(p, open):
"""HTML representation for a Parameterized object"""
title = p.__class__.name + " " + name_if_set(p)
openstr = " open" if open else ""
contents = "".join(_get_param_repr(key, val, p.param.params(key))
for key, val in p.param.get_param_values())
return (
f'<details {openstr}>\n'
' <summary style="display:list-item; outline:none;">\n'
f' <tt>{title}</tt>\n'
' </summary>\n'
' <div style="padding-left:10px;padding-bottom:5px;">\n'
' <table style="max-width:100%; border:1px solid #AAAAAA;">\n'
' <tr><th>Name</th><th>Type</th><th>Bounds</th><th>Mode</th><th>Value</th></tr>\n'
f'{contents}\n'
' </table>\n </div>\n</details>\n'
)


@add_metaclass(ParameterizedMetaclass)
Expand Down Expand Up @@ -2698,6 +2744,9 @@ def state_pop(self):
elif hasattr(g,'state_pop') and isinstance(g,Parameterized):
g.state_pop()

def _repr_html_(self, open=True):
return _parameterized_repr_html(self, open)


# API to be accessed via param namespace

Expand Down

0 comments on commit 391e4dc

Please sign in to comment.