17

I'm using a tqdm package (v4.19.5) for a loop with 200K+ iterations in Python 3.6 (Anaconda 3, Windows 10 x64, PyCharm IDE). It prints the status bar too frequently, so that my earlier history disappears in the console.

Is there a way to limit the frequency of status bar updates? I can't seem to find this solution online or tqdm website.

Currently, my status bar prints like this. Ideally, I'd like to set put a cap on percentage change. For example, update it not more frequently than once per 1% change/progress.

enter image description here

saskra
  • 15
  • 6
Oleg Melnikov
  • 3,080
  • 3
  • 34
  • 65

5 Answers5

21

You can use miniters parameter i have it from source code https://github.com/tqdm/tqdm/blob/master/tqdm/_tqdm.py

tqdm.tqdm(iterable, miniters=int(223265/100))
miroB
  • 468
  • 6
  • 8
11

The accepted answer and the cited public API

tqdm.tqdm(iterable, miniters=int(223265/100))

is correct but the code location has changed to here` https://github.com/tqdm/tqdm/blob/master/tqdm/std.py#L890-L897

Below is the description of miniters option:

miniters  : int or float, optional
    Minimum progress display update interval, in iterations.
    If 0 and `dynamic_miniters`, will automatically adjust to equal
    `mininterval` (more CPU efficient, good for tight loops).
    If > 0, will skip display of specified number of iterations.
    Tweak this and `mininterval` to get very efficient loops.
    If your progress is erratic with both fast and slow iterations
    (network, skipping items, etc) you should set miniters=1.
ascripter
  • 5,665
  • 12
  • 45
  • 68
ngalstyan
  • 637
  • 6
  • 14
7

It is convenient to use mininterval option that defines minimum time interval between updates. For instance, to update every n_seconds use:

tqdm(iterable, mininterval=n_seconds)
1

Short answer, miniters or (mininterval and maxinterval) are the way to go.

Long answer:

  • Use mininterval and maxinterval (they default to 0.1 and 10 seconds respectively)
  • Or, use miniters and maxinterval=float("inf") (see #1429 in tqdm<=4.64.1)
  • And set refresh=False in calls to set_postfix and set_postfix_str.

Three examples:

from tqdm import tqdm
from time import sleep

N = 100000

# mininterval and maxinterval in seconds
for i in tqdm(range(N), total=N, mininterval=2, maxinterval=2):
    sleep(0.3)

# miniters and maxinterval=float("inf")
for i in tqdm(range(N), total=N, miniters=50, maxinterval=float("inf")):
    sleep(0.3)

# use refresh=False in set_postfix
progress_bar = tqdm(range(N), total=N, miniters=240, maxinterval=float("inf"))
for i in progress_bar:
    sleep(0.3)
    progress_bar.set_postfix({"i": i}, refresh=False)
Marc Wouts
  • 669
  • 6
  • 8
0

I may be seeing a different problem than the one you asked about: each update to the progress bar is written to a separate new line on your end, which has been a bug of Pycharm's for years. If you run your Python script directly in a command line, there should only be a single line showing progress, and it should keep updating. Alternatively, you can set "Emulate terminal in output console" in the run/debug configurations (see screenshot attached). However, the problem might have been fixed by an update in the meantime.

Edit: Alternatively tqdm.tqdm(iterable, position=0) might also help.

Screenshot of Pycharm Run configuration

saskra
  • 15
  • 6