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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
apply flex only for nested stacks (#8357)
Shahmir Varqha886004de2c50
diff --git a/examples/outputs/stacks.py b/examples/outputs/stacks.pyindex 09eda1ce7c..98bbd4e997 100644--- a/examples/outputs/stacks.py+++ b/examples/outputs/stacks.py@@ -1,6 +1,6 @@import marimo-__generated_with = "0.19.7"+__generated_with = "0.19.11"app = marimo.App()@@ -30,5 +30,27 @@ def _(mo, n, t):return+def _(mo):+ mo.hstack(+ [+ mo.stat(value=1, bordered=True),+ mo.stat(value=2, bordered=True),+ mo.stat(value=3, bordered=True),+ ],+ widths="equal",+ )+ return+++def _(mo):+ text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Name faucibus risus in feugiat pharetra. Praesent vel ex nibh. "+ q = mo.vstack([mo.md(text)] * 5)+ s = mo.vstack([mo.md(str(0.5))] * 5, align="end", justify="space-between")+ mo.hstack([q, s], widths=[5, 1])+ return++if __name__ == "__main__":app.run()diff --git a/marimo/_plugins/stateless/flex.py b/marimo/_plugins/stateless/flex.pyindex f9f499676e..a70130652c 100644--- a/marimo/_plugins/stateless/flex.py+++ b/marimo/_plugins/stateless/flex.py@@ -13,6 +13,15 @@ if TYPE_CHECKING:from collections.abc import Sequence+class _FlexContainerHtml(Html):+ """Html produced by hstack/vstack; used to preserve justify in nested stacks.++ When this is a child of another stack with widths/heights, the wrapper+ is made a flex container so the nested stack's flex and justify work.+ Non-flex children use a block wrapper so they fill the space.+ """++def _flex(items: Sequence[object],direction: Literal["row", "column"],@@ -57,14 +66,15 @@ def _flex(child_flex = child_flexes[idx]if child_flex is None:return ""- return create_style(- {- "flex": f"{child_flex}",- "display": "flex",- "min-width": "0",- "min-height": "0",- }- )+ style: dict[str, str | int | float | None] = {"flex": f"{child_flex}"}+ # Only make the wrapper a flex container for nested stacks so their+ # flex: 1 and justify work. Leaf content (e.g. mo.stat) fills the+ # wrapper when it is a block.+ if isinstance(items[idx], _FlexContainerHtml):+ style["display"] = "flex"+ style["min-width"] = "0"+ style["min-height"] = "0"+ return create_style(style)# If there are no child flexes, don't wrap them in an additional <div>if child_flexes is None:@@ -75,7 +85,7 @@ def _flex(for i, item in enumerate(items)]- return Html(h.div(grid_items, style=style))+ return _FlexContainerHtml(h.div(grid_items, style=style))@mddocdiff --git a/tests/_plugins/stateless/test_flex.py b/tests/_plugins/stateless/test_flex.pyindex 93d1706ecf..65e0c1a409 100644--- a/tests/_plugins/stateless/test_flex.py+++ b/tests/_plugins/stateless/test_flex.py@@ -14,7 +14,7 @@ def test_vstack() -> None:result = vstack(["item1", "item2"], justify="center", heights=[1, 2])assert (result.text- == "<div style='display: flex;flex: 1;flex-direction: column;justify-content: center;align-items: normal;flex-wrap: nowrap;gap: 0.5rem'><div style='flex: 1;display: flex;min-width: 0;min-height: 0'><span>item1</span></div><div style='flex: 2;display: flex;min-width: 0;min-height: 0'><span>item2</span></div></div>" # noqa: E501+ == "<div style='display: flex;flex: 1;flex-direction: column;justify-content: center;align-items: normal;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)@@ -28,11 +28,25 @@ def test_hstack() -> None:result = hstack(["item1", "item2"], align="center", widths=[1, 2])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;display: flex;min-width: 0;min-height: 0'><span>item1</span></div><div style='flex: 2;display: flex;min-width: 0;min-height: 0'><span>item2</span></div></div>" # noqa: E501+ == "<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;display: flex;min-width: 0;min-height: 0'><span>item1</span></div><div style='flex: 1;display: flex;min-width: 0;min-height: 0'><span>item2</span></div></div>" # noqa: E501+ == "<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)+++def test_nested_stacks_preserve_flex_wrapper() -> None:+ # Nested stacks must get display:flex wrapper so their flex/justify work+ inner = vstack(["a", "b"])+ result = hstack([inner, "plain"], widths="equal")+ assert "display: flex;min-width: 0;min-height: 0" in result.text+ # First wrapper (around nested vstack) is a flex container+ assert (+ "<div style='flex: 1;display: flex;min-width: 0;min-height: 0'>"+ in result.text+ )+ # Second wrapper (around "plain") is block only so content fills width+ assert "<div style='flex: 1'><span>plain</span></div>" in result.text