Reputation: 28012
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
Upvotes: 3
Views: 19007
Reputation: 5716
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.
Upvotes: 8