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.

improvement: add hstack and vstack to arrays and dictionaries (#762)

Myles Scolnick8c8f7982adf9
diff --git a/marimo/_plugins/ui/_impl/array.py b/marimo/_plugins/ui/_impl/array.py
index b4bce055b0..98f1e983f9 100644
--- a/marimo/_plugins/ui/_impl/array.py
+++ b/marimo/_plugins/ui/_impl/array.py
@@ -4,8 +4,10 @@ from __future__ import annotations
from typing import Any, Callable, Dict, Final, Iterator, Optional, Sequence
from marimo._output.formatters.structures import format_structure
+from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._plugins.core.web_component import JSONType
+from marimo._plugins.stateless.flex import hstack, vstack
from marimo._plugins.stateless.json_output import json_output
from marimo._plugins.ui._core.ui_element import UIElement
@@ -139,3 +141,19 @@ class array(UIElement[Dict[str, JSONType], Sequence[object]]):
def __contains__(self, item: UIElement[JSONType, object]) -> bool:
return item in self.elements
+
+ def hstack(self, **kwargs: Any) -> Html:
+ """
+ Stack the elements horizontally.
+
+ For kwargs, see `marimo.hstack`.
+ """
+ return hstack(items=self.elements, **kwargs)
+
+ def vstack(self, **kwargs: Any) -> Html:
+ """
+ Stack the elements vertically.
+
+ For kwargs, see `marimo.vstack`.
+ """
+ return vstack(items=self.elements, **kwargs)
diff --git a/marimo/_plugins/ui/_impl/dictionary.py b/marimo/_plugins/ui/_impl/dictionary.py
index 54e163b64a..6a7b6c025e 100644
--- a/marimo/_plugins/ui/_impl/dictionary.py
+++ b/marimo/_plugins/ui/_impl/dictionary.py
@@ -4,7 +4,9 @@ from __future__ import annotations
from typing import Any, Callable, Optional
from marimo._output.formatters.structures import format_structure
+from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
+from marimo._plugins.stateless.flex import hstack, vstack
from marimo._plugins.stateless.json_output import json_output
from marimo._plugins.ui._core.ui_element import UIElement
from marimo._plugins.ui._impl.batch import _batch_base
@@ -126,3 +128,19 @@ class dictionary(_batch_base):
return dictionary(
self.elements, label=self._label, on_change=self._on_change
)
+
+ def hstack(self, **kwargs: Any) -> Html:
+ """
+ Stack the elements horizontally.
+
+ For kwargs, see `marimo.hstack`.
+ """
+ return hstack(items=list(self.elements.values()), **kwargs)
+
+ def vstack(self, **kwargs: Any) -> Html:
+ """
+ Stack the elements vertically.
+
+ For kwargs, see `marimo.vstack`.
+ """
+ return vstack(items=list(self.elements.values()), **kwargs)
diff --git a/marimo/_smoke_tests/arrays_and_dicts.py b/marimo/_smoke_tests/arrays_and_dicts.py
new file mode 100644
index 0000000000..2bb966700d
--- /dev/null
+++ b/marimo/_smoke_tests/arrays_and_dicts.py
@@ -0,0 +1,68 @@
+import marimo
+
+__generated_with = "0.2.3"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __(mo):
+ items = mo.ui.array(
+ [
+ mo.ui.text("name", "Name"),
+ mo.ui.text("age", "Age"),
+ mo.ui.text("email", "Email"),
+ mo.ui.text("phone", "Phone"),
+ mo.ui.text("address", "Address"),
+ mo.ui.text("memo", "Memo"),
+ ]
+ )
+ return items,
+
+
+def __(items):
+ items
+ return
+
+
+def __(items):
+ items.hstack(gap=2)
+ return
+
+
+def __(mo):
+ dictionary = mo.ui.dictionary(
+ {
+ "name": mo.ui.text("name", "Name"),
+ "age": mo.ui.text("age", "Age"),
+ "email": mo.ui.text("email", "Email"),
+ "phone": mo.ui.text("phone", "Phone"),
+ "address": mo.ui.text("address", "Address"),
+ "memo": mo.ui.text("memo", "Memo"),
+ }
+ )
+ return dictionary,
+
+
+def __(dictionary):
+ dictionary
+ return
+
+
+def __(dictionary):
+ dictionary.vstack()
+ return
+
+
+if __name__ == "__main__":
+ app.run()