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.

feat: adding initialization indicator to marimo islands (#1498)

Georgios Varnavides092dc9341114
diff --git a/marimo/_islands/island_generator.py b/marimo/_islands/island_generator.py
index adf1a3c34c..5d62cb9338 100644
--- a/marimo/_islands/island_generator.py
+++ b/marimo/_islands/island_generator.py
@@ -52,14 +52,9 @@ class MarimoIslandStub:
# Leave output accessible for direct use for non-interactive cases e.g.
# pdf.
if self._output is None:
- assert (
- self._session_view is not None
- ), "You must call build() before rendering"
- assert (
- self._internal_app is not None
- ), "You must call build() accessing output"
- outputs = self._session_view.get_cell_outputs([self._cell_id])
- self._output = outputs.get(self._cell_id, None)
+ if self._session_view is not None:
+ outputs = self._session_view.get_cell_outputs([self._cell_id])
+ self._output = outputs.get(self._cell_id, None)
return self._output
@property
@@ -358,9 +353,57 @@ class MarimoIslandGenerator:
"""
).strip()
+ def render_init_island(self) -> str:
+ """
+ Renders a static html MarimoIsland str which displays a spinning
+ initialization loader while Pyodide loads and disappears once
+ the kernel is ready to use.
+ """
+
+ init_cell_id = self._app.cell_manager.create_cell_id()
+ init_input = "<marimo-cell-code hidden> '' </marimo-cell-code>"
+ init_output = """
+ <div class="marimo" style="--tw-bg-opacity: 0;">
+ <div class="flex flex-col items-center justify-center">
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ width="24"
+ height="24"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="1"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ class="size-20 animate-spin text-primary"
+ >
+ <path d="M21 12a9 9 0 1 1-6.219-8.56"></path>
+ </svg>
+ <div>Initializing...</div>
+ </div>
+ </div>
+ """
+ init_island = dedent(
+ f"""
+ <marimo-island
+ data-app-id="{self._app_id}"
+ data-cell-id="{init_cell_id}"
+ data-reactive="{json.dumps(True)}"
+ >
+ <marimo-cell-output>
+ {init_output}
+ </marimo-cell-output>
+ {init_input}
+ </marimo-island>
+ """
+ ).strip()
+
+ return init_island
+
def render_body(
self,
*,
+ include_init_island: bool = True,
max_width: Optional[str] = None,
margin: Optional[str] = None,
style: Optional[str] = None,
@@ -370,7 +413,7 @@ class MarimoIslandGenerator:
This should be included in the <body> tag of the page.
*Args:*
-
+ - include_init_island (bool): If True, adds initialization loader.
- max_width (str): CSS style max_width property.
- margin (str): CSS style margin property.
- style (str): CSS style. Overrides max_width and margin.
@@ -380,6 +423,10 @@ class MarimoIslandGenerator:
for stub in self._stubs:
rendered_stubs.append(stub.render())
+ if include_init_island:
+ init_island = self.render_init_island()
+ rendered_stubs = [init_island] + rendered_stubs
+
body = "\n".join(rendered_stubs)
if margin is None:
@@ -409,6 +456,7 @@ class MarimoIslandGenerator:
*,
version_override: str = __version__,
_development_url: Union[str | bool] = False,
+ include_init_island: bool = True,
max_width: Optional[str] = None,
margin: Optional[str] = None,
style: Optional[str] = None,
@@ -420,6 +468,7 @@ class MarimoIslandGenerator:
- version_override (str): Marimo version to use for loaded js/css.
- _development_url (str): If True, uses local marimo islands js.
+ - include_init_island (bool): If True, adds initialization loader.
- max_width (str): CSS style max_width property.
- margin (str): CSS style margin property.
- style (str): CSS style. Overrides max_width and margin.
@@ -429,7 +478,10 @@ class MarimoIslandGenerator:
_development_url=_development_url,
)
body = self.render_body(
- max_width=max_width, margin=margin, style=style
+ include_init_island=include_init_island,
+ max_width=max_width,
+ margin=margin,
+ style=style,
)
title = (
self._app_id
diff --git a/tests/_islands/test_island_generator.py b/tests/_islands/test_island_generator.py
index f42995f393..a781f5ded8 100644
--- a/tests/_islands/test_island_generator.py
+++ b/tests/_islands/test_island_generator.py
@@ -40,15 +40,6 @@ async def test_render():
block1 = generator.add_code("import marimo as mo")
block2 = generator.add_code("mo.md('Hello, islands!')")
- # Check if render raises an error when build() is not called
- with pytest.raises(AssertionError) as e:
- block1.render()
- assert "You must call build() before rendering" in str(e.value)
-
- with pytest.raises(AssertionError) as e:
- block2.render(display_code=False)
- assert "You must call build() before rendering" in str(e.value)
-
await generator.build()
with pytest.raises(ValueError) as e: