Skip to content

Commit

Permalink
include count in html (#887)
Browse files Browse the repository at this point in the history
* include count
* add test for multicontainer html printing
  • Loading branch information
bendichter committed Jun 30, 2023
1 parent 6043e77 commit 5269c1f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/hdmf/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,13 @@ def _generate_html_repr(self, fields, level=0, access_code=".fields"):
isinstance(value, (list, dict, np.ndarray))
or hasattr(value, "fields")
):
label = key
if isinstance(value, dict):
label += f" ({len(value)})"

html_repr += (
f'<details><summary style="display: list-item; margin-left: {level * 20}px;" '
f'class="container-fields field-key" title="{current_access_code}"><b>{key}</b></summary>'
f'class="container-fields field-key" title="{current_access_code}"><b>{label}</b></summary>'
)
if hasattr(value, "fields"):
value = value.fields
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_multicontainerinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,38 @@ def test_getitem_not_found(self):
with self.assertRaisesWith(KeyError, msg):
foo['obj2']

def test_repr_html_(self):
obj1 = Container('obj1')
obj2 = Container('obj2')
foo = FooSingle()
foo.add_container([obj1, obj2])

self.assertEqual(
foo._repr_html_(),
(
'\n <style>\n .container-fields {\n font-family: "Open Sans", Arial, s'
'ans-serif;\n }\n .container-fields .field-value {\n color: #00788'
'E;\n }\n .container-fields details > summary {\n cursor: pointer;'
'\n display: list-item;\n }\n .container-fields details > summary:'
'hover {\n color: #0A6EAA;\n }\n </style>\n \n <script>'
'\n function copyToClipboard(text) {\n navigator.clipboard.writeText(text).th'
'en(function() {\n console.log(\'Copied to clipboard: \' + text);\n }'
', function(err) {\n console.error(\'Could not copy text: \', err);\n '
' });\n }\n\n document.addEventListener(\'DOMContentLoaded\', function() {\n '
' let fieldKeys = document.querySelectorAll(\'.container-fields .field-key\');\n '
' fieldKeys.forEach(function(fieldKey) {\n fieldKey.addEventListener(\'click\', fu'
'nction() {\n let accessCode = fieldKey.getAttribute(\'title\').replace(\'Access'
' code: \', \'\');\n copyToClipboard(accessCode);\n });\n '
' });\n });\n </script>\n <div class=\'container-wrap\'><div class'
'=\'container-header\'><div class=\'xr-obj-type\'><h3>FooSingle</h3></div></div><details><summary style'
'="display: list-item; margin-left: 0px;" class="container-fields field-key" title=".fields[\'container'
's\']"><b>containers (2)</b></summary><details><summary style="display: list-item; margin-left: 20px;" '
'class="container-fields field-key" title=".fields[\'containers\'][\'obj1\']"><b>obj1</b></summary></de'
'tails><details><summary style="display: list-item; margin-left: 20px;" class="container-fields field-k'
'ey" title=".fields[\'containers\'][\'obj2\']"><b>obj2</b></summary></details></details></div>'
)
)


class TestOverrideInit(TestCase):

Expand Down

0 comments on commit 5269c1f

Please sign in to comment.