Skip to content

Commit

Permalink
Add _repr_html_() as an alias of __str__()
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-tteixeira committed Nov 15, 2024
1 parent 39ca583 commit 7e38bd0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions htbuilder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ def __str__(self):
else:
return "<%(tag)s>%(children)s</%(tag)s>" % args

def _repr_html_(self):
return str(self)


def _clean_name(k):
# This allows you to use reserved words by prepending/appending underscores.
Expand Down
7 changes: 7 additions & 0 deletions tests/htbuild_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ def test_repeat(self):
<div foo="bar">hello</div>
'''))

def test_repr_html(self):
dom = div("Exists!")
self.assertEqual(
dom._repr_html_(),
normalize_whitespace('<div>Exists!</div>'),
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 7e38bd0

Please sign in to comment.