Skip to content

Non-interactive TQDMย #431

@comcon1

Description

@comcon1

TQDM should be improved to acquire TQDM_NONINTERACTIVE. Only after PR #425 will be merged.

This is a code snippet demonstrating how a non-interactive version of TQDM can be settled and used.

class LoggingTqdm(tqdm):
    def __init__(self, *args, logger=None, level=logging.INFO, **kwargs):
        self.logger = logger or logging.getLogger(__name__)
        self.level = level
        super().__init__(*args, **kwargs)

    def display(self, msg=None, pos=None):
        """Redirect tqdm's display() to the logger."""
        proc = (self.n / self.total)*100
        self.logger.log(self.level, "%8.2f%% downloaded" % proc)

pbar = LoggingTqdm(
            total=total_sz,
            desc=filename,
            unit='B',
            unit_scale=True,
            unit_divisor=1000,
            miniters=1,
            mininterval=10,
            logger=logger
        )

with requests.get(url, stream=True, headers=headers) as r, open(partial_out_path, mode) as f:
        if r.status_code not in (200, 206):
            r.raise_for_status()
        for chunk in r.iter_content(chunk_size=chunk_sz):
            f.write(chunk)
            pbar.update(len(chunk))

We don't have an agreed logging strategy yet, so print(.., file=sys.stderr) can be used instead of logging for now.

It would be useful to make it from the beginning in the following way. For example, in utils.py we could define

def fmdl_tqdm(*args, **kwargs):
    if "TQDM_NONINTERACTIVE" in os.environ:
        # do some stuff here
    else:
        return tqdm(*args, **kwargs)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions