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
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.pyindex b4bce055b0..98f1e983f9 100644--- a/marimo/_plugins/ui/_impl/array.py+++ b/marimo/_plugins/ui/_impl/array.py@@ -4,8 +4,10 @@ from __future__ import annotationsfrom typing import Any, Callable, Dict, Final, Iterator, Optional, Sequencefrom marimo._output.formatters.structures import format_structure+from marimo._output.hypertext import Htmlfrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONType+from marimo._plugins.stateless.flex import hstack, vstackfrom marimo._plugins.stateless.json_output import json_outputfrom 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.pyindex 54e163b64a..6a7b6c025e 100644--- a/marimo/_plugins/ui/_impl/dictionary.py+++ b/marimo/_plugins/ui/_impl/dictionary.py@@ -4,7 +4,9 @@ from __future__ import annotationsfrom typing import Any, Callable, Optionalfrom marimo._output.formatters.structures import format_structure+from marimo._output.hypertext import Htmlfrom marimo._output.rich_help import mddoc+from marimo._plugins.stateless.flex import hstack, vstackfrom marimo._plugins.stateless.json_output import json_outputfrom marimo._plugins.ui._core.ui_element import UIElementfrom 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.pynew file mode 100644index 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()