Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

join newlines with spaces in Html.__format__ (#3882)

Enrico Borba8b77c9d6fbdd
diff --git a/marimo/_output/hypertext.py b/marimo/_output/hypertext.py
index 515f56e8fb..d1802606b3 100644
--- a/marimo/_output/hypertext.py
+++ b/marimo/_output/hypertext.py
@@ -133,7 +133,9 @@ class Html(MIME):
def __format__(self, spec: str) -> str:
"""Format `self` as HTML text"""
del spec
- return "".join([line.strip() for line in self.text.split("\n")])
+ return " ".join(
+ [line.strip() for line in self.text.strip().split("\n")]
+ )
@mddoc
def batch(self, **elements: UIElement[JSONType, object]) -> batch_plugin:
diff --git a/tests/_output/test_hypertext.py b/tests/_output/test_hypertext.py
index 6d03d01c86..2dcb7aac68 100644
--- a/tests/_output/test_hypertext.py
+++ b/tests/_output/test_hypertext.py
@@ -23,7 +23,48 @@ def test_html_mime():
def test_html_format():
html = Html("<p>\n Hello\n</p>")
- assert f"{html}" == "<p>Hello</p>"
+ assert f"{html}" == "<p> Hello </p>"
+
+
+def test_html_format_multiline():
+ html = Html("""
+ <div>
+ <p>Hello</p>
+ <p>World</p>
+ </div>
+ """)
+ assert f"{html}" == "<div> <p>Hello</p> <p>World</p> </div>"
+
+
+def test_html_format_nested():
+ html = Html("""
+ <div>
+ <span>
+ Text
+ </span>
+ </div>
+ """)
+ assert f"{html}" == "<div> <span> Text </span> </div>"
+
+
+def test_html_format_attributes():
+ html = Html("""
+ <div class="test"
+ id="main">
+ Content
+ </div>
+ """)
+ assert f"{html}" == '<div class="test" id="main"> Content </div>'
+
+
+def test_html_format_empty():
+ html = Html("")
+ assert f"{html}" == ""
+
+
+def test_html_format_whitespace():
+ html = Html(" <p> Lots of spaces </p> ")
+ assert f"{html}" == "<p> Lots of spaces </p>"
def test_html_batch():