Reputation: 294506
How do I implement a progress bar in jupyter-notebook?
I've done this:
count = 0
max_count = 100
bar_width = 40
while count <= max_count:
time.sleep(.1)
b = bar_width * count / max_count
l = bar_width - b
print '\r' + u"\u2588" * b + '-' * l,
count += 1
Which is great when I have access to a loop in which to print stuff out. But does anyone know of anything clever to run a progress bar of some sort asynchronously?
Upvotes: 54
Views: 127513
Reputation: 8036
progressbar-2
is a modern library with some powerful features.
import time
import progressbar
for i in progressbar.progressbar(range(100)):
time.sleep(0.02)
Upvotes: 2
Reputation: 219
Another Example for Progress Bar using tqdm
from tqdm import tqdm
my_list = list(range(100))
with tqdm(total=len(my_list)) as pbar:
for x in my_list:
pbar.update(1)
Upvotes: 8
Reputation: 2823
In August 2020, the log-process widget is no longer an appropriate method to apply as it has been integrated into tqdm. The first tutorial example (using the new API) works out-of-the-box in VSCode, and I suspect it will work nicely in Jupyter as well.
from tqdm import tqdm
for i in tqdm(range(10)):
pass
Update Febuary 2021:
Tqdm attempts to detect whether or not you're in a notebook, and tries to select the appropriate type of progress bar accordingly. It does so through a module tqdm.auto
. I've observed cases where tqdm.auto
selects the command-line variant (shown above) in a VSCode notebook rather than the tqdm.notebook
variant.
This often has no ill effects, but can occsionally cause extremely verbose scrolling output. I've seen this occur while training tensorflow models in VSCode noteboks. Consequently, I'd recommend:
from tqdm import tqdm
- in library code or scriptsfrom tqdm.notebook import tqdm
- in notebooksUpvotes: 28
Reputation: 61
I have used ipywidgets and threading to implement a progress bar for the duration of a method call, see the example below
import threading
import time
import ipywidgets as widgets
def method_I_want_progress_bar_for():
progress = widgets.FloatProgress(value=0.0, min=0.0, max=1.0)
finished = False
def work(progress):#method local to this method
total = 200
for i in range(total):
if finished != True:
time.sleep(0.2)
progress.value = float(i+1)/total
else:
progress.value = 200
break
thread = threading.Thread(target=work, args=(progress,))
display(progress)
#start the progress bar thread
thread.start()
#Whatever code you want to run async
finished = True #To set the process bar to 100% and exit the thread
Upvotes: 3
Reputation: 906
Here is a solution (following this).
from ipywidgets import IntProgress
from IPython.display import display
import time
max_count = 100
f = IntProgress(min=0, max=max_count) # instantiate the bar
display(f) # display the bar
count = 0
while count <= max_count:
f.value += 1 # signal to increment the progress bar
time.sleep(.1)
count += 1
If the value that's changing in the loop is a float
instead of an int
, you can use ipwidgets.FloatProgress
instead.
Upvotes: 66
Reputation: 17349
You can try tqdm. Example code:
# pip install tqdm
from tqdm import tqdm_notebook
# works on any iterable, including cursors.
# for iterables with len(), no need to specify 'total'.
for rec in tqdm_notebook(items,
total=total,
desc="Processing records"):
# any code processing the elements in the iterable
len(rec.keys())
Demo: https://youtu.be/T0gmQDgPtzY
Upvotes: 41