rasbt / pyprind

PyPrind - Python Progress Indicator Utility
BSD 3-Clause "New" or "Revised" License
547 stars 65 forks source link

Writing the progress to a log file #20

Open rasbt opened 8 years ago

rasbt commented 8 years ago

I think in certain settings, it would be useful to redirect the progress bar/percentage to a log file (for example, if we are running scripts remotely and don't want to maintain a screen session.

An optional argument could be added that accepts a valid file path, e.g.,

ProgBar(..., logfile='path/to/logfile.log') 
ProgPercent(..., logfile='path/to/logfile.log') 
tommyip commented 7 years ago

Should the whole progress bar/percentage to be added to the log file as well? There is also a problem with this, the progress bar doesn't necessary update for every iteration so some item_id may not be included into the log file.

# Test code
def test_logging():
    bar = pyprind.ProgBar(n, logfile='./pyprind.log')
    for i in range(n):
        time.sleep(sleeptime)
        bar.update(item_id=i)

# Log file
[##                            ] | ETA: 00:00:46 | Item ID: 3
[###                           ] | ETA: 00:00:45 | Item ID: 4
[####                          ] | ETA: 00:00:43 | Item ID: 6
[#####                         ] | ETA: 00:00:41 | Item ID: 8
[######                        ] | ETA: 00:00:40 | Item ID: 9
[#######                       ] | ETA: 00:00:38 | Item ID: 11
[########                      ] | ETA: 00:00:36 | Item ID: 13
[#########                     ] | ETA: 00:00:35 | Item ID: 14
[##########                    ] | ETA: 00:00:33 | Item ID: 16
[###########                   ] | ETA: 00:00:31 | Item ID: 18
[############                  ] | ETA: 00:00:30 | Item ID: 19
[#############                 ] | ETA: 00:00:28 | Item ID: 21
[##############                ] | ETA: 00:00:26 | Item ID: 23
[###############               ] | ETA: 00:00:25 | Item ID: 24
[################              ] | ETA: 00:00:23 | Item ID: 26
[#################             ] | ETA: 00:00:21 | Item ID: 28
[##################            ] | ETA: 00:00:20 | Item ID: 29
[###################           ] | ETA: 00:00:18 | Item ID: 31
[####################          ] | ETA: 00:00:16 | Item ID: 33
[#####################         ] | ETA: 00:00:15 | Item ID: 34
[######################        ] | ETA: 00:00:13 | Item ID: 36
[#######################       ] | ETA: 00:00:11 | Item ID: 38
[########################      ] | ETA: 00:00:10 | Item ID: 39
[#########################     ] | ETA: 00:00:08 | Item ID: 41
[##########################    ] | ETA: 00:00:06 | Item ID: 43
[###########################   ] | ETA: 00:00:05 | Item ID: 44
[############################  ] | ETA: 00:00:03 | Item ID: 46
[############################# ] | ETA: 00:00:01 | Item ID: 48
[##############################] | ETA: 00:00:00 | Item ID: 49
[##############################] | ETA: 00:00:00 | Item ID: 49
rasbt commented 7 years ago

Should the whole progress bar/percentage to be added to the log file as well?

Hm, I would say yes. It may seem a bit cluttered, but it would be useful to have as much info as possible in logfiles.

There is also a problem with this, the progress bar doesn't necessary update for every iteration so some item_id may not be included into the log file.

I remember that someone mentioned this issue before ... I just see that I added a force_flush parameter at some point. It's at the bottom of the documentation:

http://nbviewer.jupyter.org/github/rasbt/pyprind/blob/master/examples/pyprind_demo.ipynb#Controlling-the-update-frequency

E.g.,

n = 100
bar = pyprind.ProgBar(n, bar_char='█')
for i in range(n):
    time.sleep(0.5) # do some computation
    bar.update(force_flush=True)

Setting force_flush=True would update the bar after each iteration (by default, there's only an update if the progress bar changes; this was done for comp. efficiency).