How can I view the progress of a program run in spyder IDE?

14,036

I often add a little progress bar into giant for-loops, to let me know how much longer I'll be waiting. I assume you wrote the script you're running, so you could do something similar.

For a very simple progress bar which only tells you that it's working, but not how far along it is, you can do

# Simple Progress Bar:
import sys  # for progress bar (sys.stdout)

for i in range(1,1000):
    # your loop's complicated code here
    sys.stdout.write('.'); sys.stdout.flush();  # print a small progress bar

(If you don't do the .flush(), it won't write any output until the whole loop is done!)

For a more complex progress bar, which actually tells me how much is left to do, I use this code:

# Full progress bar during long loop:
import sys

scan_wavelengths = range(0,1000000)  # the variable being varied
nWLs = len(scan_wavelengths)  # how many steps in the loop

# Progress Bar setup:
ProgMax = 20    # number of dots in progress bar
if nWLs<ProgMax:   ProgMax = nWLs   # if less than 20 points in scan, shorten bar
print "|" +    ProgMax*"-"    + "|     MyFunction() progress"
sys.stdout.write('|'); sys.stdout.flush();  # print start of progress bar
nProg = 0   # fraction of progress

# The main loop:
for step,wavelength   in   enumerate(scan_wavelengths):
    ''' `step` goes from 0-->N during loop'''

    #<Your complicated stuff in the loop>

    # update progress bar:
    if ( step >= nProg*nWLs/ProgMax ):
        '''Print dot at some fraction of the loop.'''
        sys.stdout.write('*'); sys.stdout.flush();  
        nProg = nProg+1
    if ( step >= nWLs-1 ):
        '''If done, write the end of the progress bar'''
        sys.stdout.write('|     done  \n'); sys.stdout.flush(); 

Hope that helps. I'm sure the many more sophisticated programmers on this site have more elegant methods for doing such things.

Share:
14,036
London guy
Author by

London guy

Passionate about Machine Learning, Analytics, Information Extraction/Retrieval and Search.

Updated on June 11, 2022

Comments

  • London guy
    London guy about 2 years

    I want to view the progress of a program while it is running in Spyder. Is it possible? As of now, I dont seem to know when it finishes unless I write a print statement at the bottom indicating that the program finished execution