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#2364: implement marimo.status.toast (#2365)

eugenecee05ccdbe8e
diff --git a/marimo/_plugins/stateless/status/__init__.py b/marimo/_plugins/stateless/status/__init__.py
index a23b0a39f0..19c2cba00f 100644
--- a/marimo/_plugins/stateless/status/__init__.py
+++ b/marimo/_plugins/stateless/status/__init__.py
@@ -1,6 +1,10 @@
# Copyright 2024 Marimo. All rights reserved.
"""Create loading indicators."""
-__all__ = ["progress_bar", "spinner"]
+__all__ = ["progress_bar", "spinner", "toast"]
-from marimo._plugins.stateless.status._progress import progress_bar, spinner
+from marimo._plugins.stateless.status._progress import (
+ progress_bar,
+ spinner,
+ toast,
+)
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.py
index 41f5a6817a..dc11bcb020 100644
--- a/marimo/_plugins/stateless/status/_progress.py
+++ b/marimo/_plugins/stateless/status/_progress.py
@@ -6,12 +6,14 @@ from typing import (
TYPE_CHECKING,
Any,
Iterable,
+ Literal,
Optional,
TypeVar,
)
import marimo._runtime.output._output as output
from marimo._messaging.mimetypes import KnownMimeType
+from marimo._messaging.ops import Alert
from marimo._output.hypertext import Html
from marimo._output.rich_help import mddoc
from marimo._plugins.core.web_component import build_stateless_plugin
@@ -307,6 +309,7 @@ class progress_bar:
- `show_rate`: if True, show the rate of progress (items per second)
- `show_eta`: if True, show the estimated time of completion
- `remove_on_exit`: if True, remove the progress bar from output on exit
+ - `disabled`: if True, disable the progress bar
"""
def __init__(
@@ -379,3 +382,31 @@ class progress_bar:
if self.remove_on_exit:
self.progress.clear()
self.progress.close()
+
+
+@mddoc
+def toast(
+ title: str,
+ description: str = "",
+ kind: Optional[Literal["danger"]] = None,
+) -> None:
+ """Show a toast notification
+
+ Use `mo.status.toast()` to display a brief notification.
+
+ **Example.**
+
+ ```python
+ mo.status.toast(
+ "Operation completed",
+ "Your task has finished successfully",
+ )
+ ```
+
+ **Args:**
+
+ - `title`: The main message of the toast
+ - `description`: Optional additional details (can include HTML)
+ - `kind`: Optional kind, use "danger" for error toasts
+ """
+ Alert(title=title, description=description, variant=kind).broadcast()
diff --git a/marimo/_smoke_tests/toast.py b/marimo/_smoke_tests/toast.py
new file mode 100644
index 0000000000..21df2f22ec
--- /dev/null
+++ b/marimo/_smoke_tests/toast.py
@@ -0,0 +1,71 @@
+import marimo
+
+__generated_with = "0.8.17"
+app = marimo.App(width="medium")
+
+
+def __():
+ import marimo as mo
+
+ return (mo,)
+
+
+def __(mo):
+ mo.md("""# Toast Notification Test""")
+ return
+
+
+def __(mo):
+ def show_toast(title, description="", kind=None):
+ mo.status.toast(title, description, kind)
+ return None
+
+ return (show_toast,)
+
+
+def __(mo):
+ simple_toast = mo.ui.checkbox(label="Simple Toast")
+ html_toast = mo.ui.checkbox(label="Toast with HTML description")
+ danger_toast = mo.ui.checkbox(label="Danger Toast")
+ return danger_toast, html_toast, simple_toast
+
+
+def __(mo):
+ mo.md("""Select a checkbox to trigger a toast notification:""")
+ return
+
+
+def __(danger_toast, html_toast, mo, simple_toast):
+ mo.vstack(
+ [
+ simple_toast,
+ html_toast,
+ danger_toast,
+ ]
+ )
+ return
+
+
+def __(danger_toast, html_toast, show_toast, simple_toast):
+ if simple_toast.value:
+ show_toast("Simple Toast", "This is a basic toast notification")
+
+ if html_toast.value:
+ show_toast(
+ "HTML Toast", "<b>Bold</b> and <i>italic</i> text in description"
+ )
+
+ if danger_toast.value:
+ show_toast("Error Occurred", "Something went wrong!", kind="danger")
+ return
+
+
+if __name__ == "__main__":
+ app.run()