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.

fix: don't render hstack/vstack in extra divs if there is no flex config (#4393)

Myles Scolnick525b111f3010
diff --git a/marimo/_plugins/stateless/flex.py b/marimo/_plugins/stateless/flex.py
index 76a67cd6d1..1b3950da2f 100644
--- a/marimo/_plugins/stateless/flex.py
+++ b/marimo/_plugins/stateless/flex.py
@@ -59,10 +59,15 @@ def _flex(
return ""
return create_style({"flex": f"{child_flex}"})
- grid_items = [
- h.div(as_html(item).text, style=create_style_for_item(i))
- for i, item in enumerate(items)
- ]
+ # If there are no child flexes, don't wrap them in an additional <div>
+ if child_flexes is None:
+ grid_items = [as_html(item).text for item in items]
+ else:
+ grid_items = [
+ h.div(as_html(item).text, style=create_style_for_item(i))
+ for i, item in enumerate(items)
+ ]
+
return Html(h.div(grid_items, style=style))
diff --git a/tests/_plugins/stateless/test_flex.py b/tests/_plugins/stateless/test_flex.py
index 3305b5c5c6..68771eb325 100644
--- a/tests/_plugins/stateless/test_flex.py
+++ b/tests/_plugins/stateless/test_flex.py
@@ -8,7 +8,7 @@ def test_vstack() -> None:
result = vstack(["item1", "item2"], align="center", gap=1)
assert (
result.text
- == "<div style='display: flex;flex: 1;flex-direction: column;justify-content: flex-start;align-items: center;flex-wrap: nowrap;gap: 1rem'><div><span>item1</span></div><div><span>item2</span></div></div>" # noqa: E501
+ == "<div style='display: flex;flex: 1;flex-direction: column;justify-content: flex-start;align-items: center;flex-wrap: nowrap;gap: 1rem'><span>item1</span><span>item2</span></div>" # noqa: E501
)
result = vstack(["item1", "item2"], justify="center", heights=[1, 2])
@@ -22,7 +22,7 @@ def test_hstack() -> None:
result = hstack(["item1", "item2"], justify="center", gap=1, wrap=True)
assert (
result.text
- == "<div style='display: flex;flex: 1;flex-direction: row;justify-content: center;align-items: normal;flex-wrap: wrap;gap: 1rem'><div><span>item1</span></div><div><span>item2</span></div></div>" # noqa: E501
+ == "<div style='display: flex;flex: 1;flex-direction: row;justify-content: center;align-items: normal;flex-wrap: wrap;gap: 1rem'><span>item1</span><span>item2</span></div>" # noqa: E501
)
result = hstack(["item1", "item2"], align="center", widths=[1, 2])
@@ -30,3 +30,9 @@ def test_hstack() -> None:
result.text
== "<div style='display: flex;flex: 1;flex-direction: row;justify-content: space-between;align-items: center;flex-wrap: nowrap;gap: 0.5rem'><div style='flex: 1'><span>item1</span></div><div style='flex: 2'><span>item2</span></div></div>" # noqa: E501
)
+
+ result = hstack(["item1", "item2"], align="center", widths="equal")
+ assert (
+ result.text
+ == "<div style='display: flex;flex: 1;flex-direction: row;justify-content: space-between;align-items: center;flex-wrap: nowrap;gap: 0.5rem'><div style='flex: 1'><span>item1</span></div><div style='flex: 1'><span>item2</span></div></div>" # noqa: E501
+ )
diff --git a/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt b/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt
index db15bc778c..605be4dd52 100644
--- a/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt
+++ b/tests/_server/export/snapshots/notebook_with_outputs.ipynb.txt
@@ -163,7 +163,7 @@
{
"data": {
"text/html": [
- "<div style='display: flex;flex: 1;flex-direction: column;justify-content: flex-start;align-items: normal;flex-wrap: nowrap;gap: 0.5rem'><div><span class=\"markdown prose dark:prose-invert\"><span class=\"paragraph\">hello</span></span></div><div><span class=\"markdown prose dark:prose-invert\"><span class=\"paragraph\">world</span></span></div></div>"
+ "<div style='display: flex;flex: 1;flex-direction: column;justify-content: flex-start;align-items: normal;flex-wrap: nowrap;gap: 0.5rem'><span class=\"markdown prose dark:prose-invert\"><span class=\"paragraph\">hello</span></span><span class=\"markdown prose dark:prose-invert\"><span class=\"paragraph\">world</span></span></div>"
]
},
"metadata": {},