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: expand tqdm support (#4765)

brenocaf2cf61497a74
diff --git a/marimo/_output/formatters/tqdm_formatters.py b/marimo/_output/formatters/tqdm_formatters.py
index a6b5b50a5d..a05c52bcf5 100644
--- a/marimo/_output/formatters/tqdm_formatters.py
+++ b/marimo/_output/formatters/tqdm_formatters.py
@@ -26,7 +26,7 @@ class ProgressBarTqdmPatch(progress_bar):
super().__init__(
collection=iterable,
- title=desc or "Loading...",
+ title=desc or "",
total=total,
)
@@ -39,6 +39,38 @@ class ProgressBarTqdmPatch(progress_bar):
if hasattr(self, "progress") and self.progress is not None:
self.progress.update(increment=n)
+ def close(self) -> None:
+ """Close the progress bar and clean up.
+
+ This method is called when the progress bar is no longer needed.
+ In tqdm, this method also handles styling based on completion status.
+ """
+ if hasattr(self, "progress") and self.progress is not None:
+ self.progress.clear()
+ self.progress.close()
+
+ @classmethod
+ def write(cls, s: str, file: Any = None, end: str = "\n") -> None:
+ """Print a message via tqdm (without overlap with bars).
+
+ Args:
+ s (str): The message to print
+ file: The file to write to (defaults to sys.stdout)
+ end (str): The end character to use (defaults to newline)
+ """
+ import sys
+
+ fp: Any = file if file is not None else sys.stdout
+ # In marimo, we don't need special handling to avoid overlapping with bars
+ # as the output is handled differently than in terminal environments
+ fp.write(s)
+ fp.write(end)
+
+
+class ProgressBarTrangePatch(ProgressBarTqdmPatch):
+ def __init__(self, *args: Any, **kwargs: Any):
+ super().__init__(range(*args), **kwargs)
+
class TqdmFormatter(FormatterFactory):
@staticmethod
@@ -47,6 +79,8 @@ class TqdmFormatter(FormatterFactory):
def register(self) -> None:
if running_in_notebook():
+ # Import tqdm.notebook for notebook-specific progress bar implementation
import tqdm.notebook # type: ignore [import-not-found,import-untyped] # noqa: E501
tqdm.notebook.tqdm = ProgressBarTqdmPatch
+ tqdm.notebook.trange = ProgressBarTrangePatch
diff --git a/marimo/_smoke_tests/tqdm_update_test.py b/marimo/_smoke_tests/tqdm_update_test.py
index df0cd7b100..249301fcae 100644
--- a/marimo/_smoke_tests/tqdm_update_test.py
+++ b/marimo/_smoke_tests/tqdm_update_test.py
@@ -7,9 +7,9 @@ app = marimo.App(width="medium")
@app.cell
def __():
- from tqdm.notebook import tqdm
+ from tqdm.notebook import tqdm, trange
import time
- return time, tqdm
+ return time, tqdm, trange
@app.cell
@@ -19,12 +19,19 @@ def __(time, tqdm):
time.sleep(0.1)
return i,
+def __(time, tqdm):
+ # Test regular iteration
+ for i in trange(5):
+ time.sleep(0.1)
+ return i,
+
@app.cell
def __(time, tqdm):
# Test manual update method
- pbar = tqdm(total=10)
- for i in range(10):
+ pbar = tqdm(total=5)
+ for i in range(5):
time.sleep(0.1)
pbar.update(1) # Explicitly calling update
pbar.close()
@@ -34,10 +41,10 @@ def __(time, tqdm):
@app.cell
def __(time, tqdm):
# Test update with different increment
- pbar = tqdm(total=100)
- for i in range(0, 100, 10):
+ pbar = tqdm(total=50)
+ for i in range(0, 50, 5):
time.sleep(0.1)
- pbar.update(10) # Update by 10 each time
+ pbar.update(5) # Update by 5 each time
pbar.close()
return i,