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
faq: interrupt a progress bar (#8406)
Akshay Agrawal7da1cf88addc
diff --git a/docs/faq.md b/docs/faq.mdindex 3e22175e10..4f82e5e9a6 100644--- a/docs/faq.md+++ b/docs/faq.md@@ -315,6 +315,71 @@ use [`mo.ui.array`][marimo.ui.array] orFor usage examples, see the[recipes for grouping UI elements together](recipes.md#grouping-ui-elements-together).++### How do I let users interrupt a progress bar iteration?++To create an interruptible progress bar, run the progress bar in its own thread,+and create a button that on change signals to the thread that it should exit.++Example:++```python+import marimo++__generated_with = "0.20.1"+app = marimo.App()+++def _():+ import marimo as mo+ import time+ from threading import Event+ return Event, mo, time+++def _(Event):+ cancelled = Event()+ return (cancelled,)+++def _(cancelled, mo):+ cancel = mo.ui.button(+ label="Interrupt the progress bar", on_change=lambda _: cancelled.set()+ )+ cancel+ return+++def _(cancelled, mo, time):+ def progress(total):+ with mo.status.progress_bar(total=10) as pbar:+ for _ in range(10):+ if cancelled.is_set():+ pbar.update(+ increment=0, subtitle="The user cancelled the iteration"+ )+ break+ # Sleep... or anything else that releases GIL+ time.sleep(0.5)+ pbar.update()++ return (progress,)+++def _(mo, progress):+ mo.Thread(target=progress, args=(10,)).start()+ return+++if __name__ == "__main__":+ app.run()+```+<a name="faq-restart"></a>### How do I restart a notebook?