Base class

class ProgressBar[source]

ProgressBar(gen=None, total=None, display=True, leave=True, parent=None, master=None, comment='')

class VerboseProgressBar(ProgressBar):
    def on_iter_begin(self): super().on_iter_begin(); print("on_iter_begin")
    def on_interrupt(self):  print("on_interrupt")
    def on_iter_end(self):   print("on_iter_end"); super().on_iter_end()
    def on_update(self, val, text): print(f"on_update {val}")
from contextlib import redirect_stdout
import io
tst_pb = VerboseProgressBar(range(6))
s = io.StringIO()
with redirect_stdout(s): 
    for i in tst_pb: time.sleep(0.1)

assert s.getvalue() == '\n'.join(['on_iter_begin'] + [f'on_update {i}' for i in range(7)] + ['on_iter_end']) + '\n'
tst_pb = VerboseProgressBar(range(6))
s = io.StringIO()
with redirect_stdout(s): 
    for i in range(7): 
        tst_pb.update(i)
        time.sleep(0.1)

assert s.getvalue() == '\n'.join(['on_iter_begin'] + [f'on_update {i}' for i in range(7)] + ['on_iter_end']) + '\n'

class MasterBar[source]

MasterBar(gen, cls, total=None) :: ProgressBar

class VerboseMasterBar(MasterBar):
    def __init__(self, gen, total=None): super().__init__(gen, VerboseProgressBar, total=total)
    def on_iter_begin(self): print("master_on_iter_begin")
    def on_interrupt(self):  print("master_on_interrupt")
    def on_iter_end(self):   print("master_on_iter_end")
    #def on_update(self, val, text): print(f"master_on_update {val}")
tst_mb = VerboseMasterBar(range(6))
for i in tst_mb: time.sleep(0.1)
master_on_iter_begin
on_iter_begin
on_update 0
on_update 1
on_update 2
on_update 3
on_update 4
on_update 5
on_update 6
on_iter_end
master_on_iter_end

Notebook progress bars

class NBProgressBar[source]

NBProgressBar(gen=None, total=None, display=True, leave=True, parent=None, master=None, comment='') :: ProgressBar

tst = NBProgressBar(range(100))
for i in tst: time.sleep(0.05)
100.00% [100/100 00:05<00:00]
tst = NBProgressBar(range(100))
for i in range(50): 
    time.sleep(0.05)
    tst.update(i)
tst.on_interrupt()
Interrupted

class NBMasterBar[source]

NBMasterBar(gen, total=None, hide_graph=False, order=None, clean_on_interrupt=False, total_time=False) :: MasterBar

mb = NBMasterBar(range(5))
for i in mb:
    for j in NBProgressBar(range(10), parent=mb, comment=f'first bar stat'):
        time.sleep(0.01)
        #mb.child.comment = f'second bar stat'
    mb.write(f'Finished loop {i}.')
Finished loop 0.

Finished loop 1.

Finished loop 2.

Finished loop 3.

Finished loop 4.

mb = NBMasterBar(range(5))
mb.update(0)  
for i in range(5):
    for j in NBProgressBar(range(10), parent=mb):
        time.sleep(0.01)
        #mb.child.comment = f'second bar stat'
    mb.main_bar.comment = f'first bar stat'
    mb.write(f'Finished loop {i}.')
    mb.update(i+1)
Finished loop 0.

Finished loop 1.

Finished loop 2.

Finished loop 3.

Finished loop 4.

Console progress bars

printing[source]

printing()

True if we want to print progress

class ConsoleProgressBar[source]

ConsoleProgressBar(gen, total=None, display=True, leave=True, parent=None, master=None, txt_len=60) :: ProgressBar

tst = ConsoleProgressBar(range(100))
for i in tst: time.sleep(0.05)
 |████████████████████████████████████████| 100.00% [100/100 00:05<00:00]
tst = ConsoleProgressBar(range(100))
for i in range(50): 
    time.sleep(0.05)
    tst.update(i)
tst.on_interrupt()
 |██████████████████----------------------| 47.00% [47/100 00:02<00:02]

print_and_maybe_save(line)

class ConsoleMasterBar[source]

ConsoleMasterBar(gen, total=None, hide_graph=False, order=None, clean_on_interrupt=False, total_time=False) :: MasterBar

mb = ConsoleMasterBar(range(5))
for i in mb:
    for j in ConsoleProgressBar(range(10), parent=mb):
        time.sleep(0.01)
        #mb.child.comment = f'second bar stat'
    mb.main_bar.comment = f'first bar stat'
    mb.write(f'Finished loop {i}.')
Finished loop 0.                                                                  
Finished loop 1.                                                                  
Finished loop 2.                                                                  
Finished loop 3.                                                                  
Finished loop 4.                                                                  
mb = ConsoleMasterBar(range(5))
mb.update(0)  
for i in range(5):
    for j in ConsoleProgressBar(range(10), parent=mb):
        time.sleep(0.01)
        #mb.child.comment = f'second bar stat'
    mb.main_bar.comment = f'first bar stat'
    mb.write(f'Finished loop {i}.')
    mb.update(i+1)
    
    # confirming a kwarg can be passed to ConsoleMasterBar instance
    mb.update_graph([[1,2],[3,4]], figsize=(10,5,))
    mb.show_imgs(figsize=(10,5,))
Finished loop 0.                                                                  
Finished loop 1.                                                                  
Finished loop 2.                                                                  
Finished loop 3.                                                                  
Finished loop 4.                                                                  

force_console_behavior[source]

force_console_behavior()

Return the console progress bars

workaround_empty_console_output[source]

workaround_empty_console_output()

Change console output behaviour to correctly show progress in consoles not recognizing at the end of line

from nbdev.export import notebook2script
notebook2script()
Converted 00_core.ipynb.
Converted 01_fastprogress.ipynb.