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.

support tqdm.notebook (#1922)

Akshay Agrawal2cfe01a96c74
diff --git a/marimo/_output/formatters/formatters.py b/marimo/_output/formatters/formatters.py
index f1096f6bcb..36b4684cc5 100644
--- a/marimo/_output/formatters/formatters.py
+++ b/marimo/_output/formatters/formatters.py
@@ -21,6 +21,7 @@ from marimo._output.formatters.pandas_formatters import PandasFormatter
from marimo._output.formatters.plotly_formatters import PlotlyFormatter
from marimo._output.formatters.seaborn_formatters import SeabornFormatter
from marimo._output.formatters.structures import StructuresFormatter
+from marimo._output.formatters.tqdm_formatters import TqdmFormatter
# Map from formatter factory's package name to formatter, for third-party
# modules. These formatters will be registered if and when their associated
@@ -38,6 +39,7 @@ THIRD_PARTY_FACTORIES: dict[str, FormatterFactory] = {
HoloViewsFormatter.package_name(): HoloViewsFormatter(),
IPythonFormatter.package_name(): IPythonFormatter(),
AnyWidgetFormatter.package_name(): AnyWidgetFormatter(),
+ TqdmFormatter.package_name(): TqdmFormatter(),
}
# Formatters for builtin types and other things that don't require a
diff --git a/marimo/_output/formatters/tqdm_formatters.py b/marimo/_output/formatters/tqdm_formatters.py
new file mode 100644
index 0000000000..4ca3e750a5
--- /dev/null
+++ b/marimo/_output/formatters/tqdm_formatters.py
@@ -0,0 +1,41 @@
+# Copyright 2024 Marimo. All rights reserved.
+from __future__ import annotations
+
+from typing import Any
+
+from marimo._output.formatters.formatter_factory import FormatterFactory
+from marimo._plugins.stateless.status._progress import progress_bar
+from marimo._runtime.context.utils import running_in_notebook
+
+
+class TqdmFormatter(FormatterFactory):
+ @staticmethod
+ def package_name() -> str:
+ return "tqdm"
+
+ def register(self) -> None:
+ if running_in_notebook():
+ import tqdm.notebook # type: ignore [import-not-found,import-untyped] # noqa: E501
+
+ def tqdm_to_progress_bar(
+ *args: Any, **kwargs: Any
+ ) -> progress_bar:
+ # Partial translation from tqdm to our native progress bar;
+ # uses API of tqdm v4.66.4, likely backward compatible.
+ iterable: Any = kwargs.get("iterable", None)
+ desc: str | None = kwargs.get("desc", None)
+ total: int | None = kwargs.get("total", None)
+
+ # In case args were used
+ if args:
+ iterable = args[0]
+ if len(args) >= 2:
+ desc = args[1]
+ if len(args) >= 3:
+ total = args[2]
+
+ return progress_bar(
+ collection=iterable, title=desc, total=total
+ )
+
+ tqdm.notebook.tqdm = tqdm_to_progress_bar
diff --git a/marimo/_smoke_tests/tqdm_notebook.py b/marimo/_smoke_tests/tqdm_notebook.py
new file mode 100644
index 0000000000..f7e03f64da
--- /dev/null
+++ b/marimo/_smoke_tests/tqdm_notebook.py
@@ -0,0 +1,24 @@
+# Copyright 2024 Marimo. All rights reserved.
+import marimo
+
+__generated_with = "0.7.13"
+app = marimo.App(width="medium")
+
+
+def __():
+ from tqdm.notebook import tqdm
+
+ import time
+ return time, tqdm
+
+
+def __(time, tqdm):
+ for i in tqdm(range(10)):
+ time.sleep(0.1)
+ return i,
+
+
+if __name__ == "__main__":
+ app.run()