Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change logging "print" function to "tqdm.write" so logging doesn't interfere with progress bars

Tags:

I have a simple question: How do I change the built-in Python logger's print function to tqdm.write such that logging messages do not interfere with tqdm's progress bars? Thanks!

like image 836
Guillochon Avatar asked Jul 23 '16 15:07

Guillochon


People also ask

How do I get rid of progress bar in tqdm?

There is a disable argument which you can set to True to silence any tqdm output (and in fact it will totally skip the progress bar calculations too, not just the display). To dynamically switch it, you can just add a commandline argument to your script that will define if disable is set or not.

Does tqdm affect performance?

In addition, a huge benefit of using tqdm instead of a different method for showing a progress bar is that tqdm has little overhead, around 60 nanoseconds per iteration — meaning it should not affect performance much, compared to something like ProgressBar, which has an overhead of 800 nanoseconds per iteration.

What is tqdm progress bar?

In Arabic, tqdm (taqadum) means progress, and it is used to create a smart progress bar for the loops. You just need to wrap tqdm on any iterable - tqdm(iterable).

Does tqdm work with while loops?

tqdm does not require any dependencies and works across multiple python environments. Integrating tqdm can be done effortlessly in loops, on iterable, with Pandas or even with machine learning libraries— just wrap any iterable with tqdm(iterable) , and you're done!


1 Answers

You need a custom logging handler:

import logging import tqdm  class TqdmLoggingHandler(logging.Handler):     def __init__(self, level=logging.NOTSET):         super().__init__(level)      def emit(self, record):         try:             msg = self.format(record)             tqdm.tqdm.write(msg)             self.flush()         except Exception:             self.handleError(record)   

and then add this to the logging chain:

import time  log = logging.getLogger(__name__) log.setLevel(logging.INFO) log.addHandler(TqdmLoggingHandler()) for i in tqdm.tqdm(range(100)):     if i == 50:         log.info("Half-way there!")     time.sleep(0.1) 

Edit: Fixed a bug in the call to super TqdmLoggingHandler's init method, that was pointed out by diligent reader @BlaineRogers in the comments. (If anyone wants to read further about this murky area of Python, I recommend https://fuhm.net/super-harmful/)

like image 88
RolKau Avatar answered Sep 20 '22 10:09

RolKau