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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: debounce mo.status.progress when flusing to the UI (#856)
Myles Scolnickc8b7e0afa156
diff --git a/marimo/_plugins/stateless/status/_progress.py b/marimo/_plugins/stateless/status/_progress.pyindex a4ca3b63b5..efad09e267 100644--- a/marimo/_plugins/stateless/status/_progress.py+++ b/marimo/_plugins/stateless/status/_progress.py@@ -10,6 +10,7 @@ import marimo._runtime.output._output as outputfrom marimo._output.hypertext import Htmlfrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import build_stateless_plugin+from marimo._utils.debounce import debounceS = TypeVar("S")T = TypeVar("T")@@ -82,6 +83,11 @@ class _Progress(Html):self.subtitle = subtitleself._text = self._get_text()+ self.debounced_flush()++ @debounce(0.15)+ def debounced_flush(self) -> None:+ """Flush the output to the UI"""output.flush()def clear(self) -> None:@@ -93,6 +99,7 @@ class _Progress(Html):output.remove(self)def close(self) -> None:+ output.flush() # Flush one last time before closingself.closed = Truedef _get_text(self) -> str:@@ -257,6 +264,9 @@ def progress_bar(You can optionally provide a title and subtitle to showduring iteration, and a title/subtitle to show upon completion.+ For performance reasons, the progress bar is only updated in the UI+ every 150ms.+**Args.**- `collection`: a collection to iterate overdiff --git a/marimo/_smoke_tests/status.py b/marimo/_smoke_tests/status.pyindex 02ff19ea6d..7fca4a3f2d 100644--- a/marimo/_smoke_tests/status.py+++ b/marimo/_smoke_tests/status.py@@ -1,7 +1,8 @@# Copyright 2024 Marimo. All rights reserved.+import marimo-__generated_with = "0.1.63"+__generated_with = "0.2.9"app = marimo.App()@@ -24,7 +25,11 @@ def __(mo, time):@app.celldef __(mo, time):for _ in mo.status.progress_bar(- range(10), title="Loading", subtitle="Please wait", show_eta=True, show_rate=True+ range(10),+ title="Loading",+ subtitle="Please wait",+ show_eta=True,+ show_rate=True,):time.sleep(0.5)return@@ -49,5 +54,13 @@ def __(mo, time):return+def __(mo, time):+ # Fast updates should be debounced for performance+ for i in mo.status.progress_bar(range(1000)):+ time.sleep(0.001)+ return i,++if __name__ == "__main__":app.run()diff --git a/marimo/_utils/debounce.py b/marimo/_utils/debounce.pynew file mode 100644index 0000000000..d90aadb7c1--- /dev/null+++ b/marimo/_utils/debounce.py@@ -0,0 +1,27 @@+import time+from functools import wraps+from typing import Any, Callable, TypeVar, cast++F = TypeVar("F", bound=Callable[..., None])+++def debounce(wait_time: float) -> Callable[[F], F]:+ """+ Decorator to prevent a function from being called more than once every+ wait_time seconds.+ """++ def decorator(func: F) -> F:+ last_called: float = 0++ @wraps(func)+ def wrapped(*args: Any, **kwargs: Any) -> None:+ nonlocal last_called+ current_time = time.time()+ if current_time - last_called >= wait_time:+ last_called = current_time+ func(*args, **kwargs)++ return cast(F, wrapped)++ return decoratordiff --git a/tests/_utils/test_decorator.py b/tests/_utils/test_decorator.pynew file mode 100644index 0000000000..919d848192--- /dev/null+++ b/tests/_utils/test_decorator.py@@ -0,0 +1,41 @@+# Assuming the debounce decorator is defined in a file named debounce.py+import time++from marimo._utils.debounce import debounce++# A simple function for testing purposes+call_count = 0+++def test_debounce_within_period():+ @debounce(wait_time=1.5)+ def my_function():+ global call_count+ call_count += 1++ global call_count+ call_count = 0 # Reset the counter before the test++ my_function() # Call the function for the first time+ assert call_count == 1++ time.sleep(0.1) # Wait less than the debounce period+ my_function() # Attempt to call the function again+ assert call_count == 1+++def test_debounce_after_period():+ @debounce(wait_time=0.1)+ def my_function():+ global call_count+ call_count += 1++ global call_count+ call_count = 0 # Reset the counter before the test++ my_function() # Call the function for the first time+ assert call_count == 1++ time.sleep(0.4) # Wait more than the debounce period+ my_function() # Attempt to call the function again+ assert call_count == 2