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: extend support to tqdm update() (#4623)

brenoca30192fc7b598
diff --git a/marimo/_output/formatters/tqdm_formatters.py b/marimo/_output/formatters/tqdm_formatters.py
index 9b7dc64567..a6b5b50a5d 100644
--- a/marimo/_output/formatters/tqdm_formatters.py
+++ b/marimo/_output/formatters/tqdm_formatters.py
@@ -30,6 +30,15 @@ class ProgressBarTqdmPatch(progress_bar):
total=total,
)
+ def update(self, n: int = 1) -> None:
+ """Update the progress bar by incrementing it by n.
+
+ Args:
+ n (int, optional): Number of iterations to increment by. Defaults to 1.
+ """
+ if hasattr(self, "progress") and self.progress is not None:
+ self.progress.update(increment=n)
+
class TqdmFormatter(FormatterFactory):
@staticmethod
diff --git a/marimo/_smoke_tests/tqdm_update_test.py b/marimo/_smoke_tests/tqdm_update_test.py
new file mode 100644
index 0000000000..df0cd7b100
--- /dev/null
+++ b/marimo/_smoke_tests/tqdm_update_test.py
@@ -0,0 +1,46 @@
+# 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):
+ # Test regular iteration
+ for i in tqdm(range(5)):
+ time.sleep(0.1)
+ return i,
+
+
+def __(time, tqdm):
+ # Test manual update method
+ pbar = tqdm(total=10)
+ for i in range(10):
+ time.sleep(0.1)
+ pbar.update(1) # Explicitly calling update
+ pbar.close()
+ return i,
+
+
+def __(time, tqdm):
+ # Test update with different increment
+ pbar = tqdm(total=100)
+ for i in range(0, 100, 10):
+ time.sleep(0.1)
+ pbar.update(10) # Update by 10 each time
+ pbar.close()
+ return i,
+
+
+if __name__ == "__main__":
+ app.run()