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(status): Add async progress bar functionality (#6336)

Daizube3e52b30e56
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.py
index 7f387ff05c..e629eaab4c 100644
--- a/marimo/_plugins/stateless/status/_progress.py
+++ b/marimo/_plugins/stateless/status/_progress.py
@@ -2,7 +2,7 @@
from __future__ import annotations
import time
-from collections.abc import Sized
+from collections.abc import AsyncIterable, Iterable, Sized
from typing import (
TYPE_CHECKING,
Any,
@@ -23,7 +23,7 @@ from marimo._plugins.core.web_component import build_stateless_plugin
from marimo._utils.debounce import debounce
if TYPE_CHECKING:
- from collections.abc import Collection, Iterator
+ from collections.abc import AsyncIterator, Collection, Iterator
S = TypeVar("S")
T = TypeVar("T")
@@ -303,7 +303,7 @@ class progress_bar(Generic[S]):
every 150ms.
Args:
- collection (Union[Collection[S], Iterator[S]], optional): Optional collection to iterate over.
+ collection (Union[Collection[S], Iterator[S], AsyncIterable[S]], optional): Optional collection to iterate over.
title (str, optional): Optional title.
subtitle (str, optional): Optional subtitle.
completion_title (str, optional): Optional title to show during completion.
@@ -334,7 +334,7 @@ class progress_bar(Generic[S]):
@overload
def __init__(
self,
- collection: Iterator[S] = ...,
+ collection: Iterator[S] | AsyncIterable[S] = ...,
*,
title: Optional[str] = ...,
subtitle: Optional[str] = ...,
@@ -365,7 +365,9 @@ class progress_bar(Generic[S]):
def __init__(
self,
- collection: Optional[Collection[S] | Iterator[S]] = None,
+ collection: Optional[
+ Collection[S] | Iterator[S] | AsyncIterable[S]
+ ] = None,
*,
title: Optional[str] = None,
subtitle: Optional[str] = None,
@@ -382,17 +384,20 @@ class progress_bar(Generic[S]):
self.remove_on_exit = remove_on_exit
self.disabled = disabled
self.step: int = 1
+ self.collection = collection
+ self._is_async = isinstance(collection, AsyncIterable)
if collection is not None:
- self.collection: Optional[Collection[S] | Iterator[S]] = collection
-
- if not isinstance(collection, Sized):
- # if collection is a generator
- raise TypeError(
- "fail to determine length of collection, use `total`"
- + "to specify"
- ) from None
- total = total or len(collection)
+ if total is None:
+ if isinstance(collection, Sized):
+ total = len(collection)
+
+ else:
+ raise TypeError(
+ "Cannot determine the length of a collection. "
+ "A `total` must be provided."
+ )
+
if isinstance(collection, range):
self.step = cast(range, collection).step
@@ -401,9 +406,6 @@ class progress_bar(Generic[S]):
"`total` is required when using as a context manager"
)
- else:
- self.collection = None
-
self.progress = ProgressBar(
title=title,
subtitle=subtitle,
@@ -419,11 +421,40 @@ class progress_bar(Generic[S]):
raise RuntimeError(
"progress_bar can only be iterated over if a collection is provided"
)
- for item in self.collection:
- yield item
- if not self.disabled:
- self.progress.update(increment=self.step)
- self._finish()
+
+ if self._is_async:
+ raise RuntimeError(
+ "Cannot iterate over an async collection with `for`. "
+ "Use `async for` instead."
+ )
+
+ try:
+ for item in cast(Iterable[S], self.collection):
+ yield item
+ if not self.disabled:
+ self.progress.update(increment=self.step)
+ finally:
+ self._finish()
+
+ async def __aiter__(self) -> AsyncIterator[S]:
+ if self.collection is None:
+ raise RuntimeError(
+ "progress_bar can only be iterated over if a collection is provided"
+ )
+
+ if not self._is_async:
+ raise RuntimeError(
+ "Cannot iterate over a sync collection with `async for`. "
+ "Use `for` instead."
+ )
+
+ try:
+ async for item in cast(AsyncIterable[S], self.collection):
+ yield item
+ if not self.disabled:
+ self.progress.update(increment=self.step)
+ finally:
+ self._finish()
def __enter__(self) -> ProgressBar:
return self.progress
diff --git a/tests/_plugins/stateless/status/test_progress.py b/tests/_plugins/stateless/status/test_progress.py
index af227e7c0c..a064d470f6 100644
--- a/tests/_plugins/stateless/status/test_progress.py
+++ b/tests/_plugins/stateless/status/test_progress.py
@@ -132,3 +132,85 @@ def test_progress_without_context():
with pytest.raises(RuntimeError):
for _ in progress_bar(total=10):
pass
+
+
+async def sleep(seconds):
+ import asyncio
+
+ tasks = [asyncio.create_task(asyncio.sleep(s, s)) for s in seconds]
+ for future in asyncio.as_completed(tasks):
+ yield await future
+
+
+async def test_progress_async():
+ assert runtime_context_installed() is False
+
+ ait = sleep([0.01, 0.003, 0.001])
+ result = [s async for s in progress_bar(ait, total=3)]
+ assert result == [0.001, 0.003, 0.01]
+
+
+def test_progress_no_total_error():
+ assert runtime_context_installed() is False
+
+ def sync_generator():
+ yield 1
+ yield 2
+ yield 3
+
+ with pytest.raises(
+ TypeError,
+ match="Cannot determine the length of a collection. A `total` must be provided.",
+ ):
+ progress_bar(sync_generator())
+
+
+def test_progress_async_no_total_error():
+ assert runtime_context_installed() is False
+
+ async def async_generator():
+ yield 1
+ yield 2
+ yield 3
+
+ with pytest.raises(
+ TypeError,
+ match="Cannot determine the length of a collection. A `total` must be provided.",
+ ):
+ progress_bar(async_generator())
+
+
+def test_progress_for_loop_error():
+ assert runtime_context_installed() is False
+
+ async def async_generator():
+ yield 1
+
+ with pytest.raises(
+ RuntimeError,
+ match="Cannot iterate over an async collection with `for`. Use `async for` instead.",
+ ):
+ for _ in progress_bar(async_generator(), total=1):
+ pass
+
+
+async def test_progress_async_for_loop_error():
+ assert runtime_context_installed() is False
+
+ with pytest.raises(
+ RuntimeError,
+ match="Cannot iterate over a sync collection with `async for`. Use `for` instead.",
+ ):
+ async for _ in progress_bar([1, 2, 3]):
+ pass
+
+
+async def test_progress_async_for_loop_without_collection_error():
+ assert runtime_context_installed() is False
+
+ with pytest.raises(
+ RuntimeError,
+ match="progress_bar can only be iterated over if a collection is provided",
+ ):
+ async for _ in progress_bar(total=1):
+ pass