Skip to content

Commit

Permalink
Example code for ineterminate progress bar with a long running comput…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
GenevieveBuckley committed Sep 5, 2023
1 parent a65a59a commit d751b99
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions examples/progress_indeterminate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import time

from superqt.utils import thread_worker

from magicgui import magicgui
from magicgui.tqdm import tqdm


@magicgui(call_button=True, layout="horizontal")
def long_running(sleep_time=5):
"""Long running computation with an indeterminate progress bar."""
# Here tqdm is not provided an iterable argument, or the 'total' kwarg
# so it cannot calculate the expected number of iterations
# which means it will create an indeterminate progress bar
with tqdm() as pbar:
# It is best practice to use a separate thread for long running computations
# This makes the function non-blocking, you can still interact with the widget
@thread_worker(connect={"finished": lambda: pbar.progressbar.hide()})
def sleep(secs):
time.sleep(secs)

sleep(sleep_time)


long_running.show(run=True)

0 comments on commit d751b99

Please sign in to comment.