tqdm / tqdm

:zap: A Fast, Extensible Progress Bar for Python and CLI

Home Page:https://tqdm.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Proposal: setting of info, without needing `pbar` object

sradc opened this issue · comments

  • I have marked all applicable categories:
    • documentation request (i.e. "X is missing from the documentation." If instead I want to ask "how to use X?" I understand [StackOverflow#tqdm] is more appropriate)
    • new feature request
  • I have visited the [source website], and in particular
    read the [known issues]
  • I have searched through the [issue tracker] for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable:
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)

progress bar info can be set in two ways:

  1. With a context manager. This introduces extra indententation, and an extra line.
with tqdm(["a", "b", "c", "d"]) as pbar:
    for item in pbar:
        pbar.set_description(...)
        pbar.set_postfix(...)
  1. With a pbar variable. This introduces two extra lines.
pbar = tqdm(["a", "b", "c", "d"])
for item in pbar:
    pbar.set_description(...)
    pbar.set_postfix(...)
pbar.close()

The proposal here is for a third alternative, which would be:

for item in tqdm(["a", "b", "c", "d"]):
    tqdm.set_description(...)
    tqdm.set_postfix(...)

Where tqdm.set_description and tqdm.set_postfix set a value, and then the next pbar to update takes these values.

It is slightly magical, but reminds me a bit of of the matplotlib.pyplot stateful API versus the object oriented one; where the stateful api is often more convenient for quick simple things.

I think it would help to simplify and reduce clutter in loops.

Edit:

Ah, there's a third way:

for item in (pbar := tqdm(["a", "b", "c", "d"])):
    pbar.set_description(...)
    pbar.set_postfix(...)

Maybe this is preferable to the above proposal; although it is perhaps somewhat more complex looking, it's not so bad.