rudolfbyker · GitHub

  • I have marked all applicable categories:
    • exception-raising bug
    • visual output bug
  • 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)

Versions

  • tqdm version: 4.67.1
  • Python version: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:17:27) [MSC v.1929 64 bit (AMD64)]
  • Operating system: win32

Facts

The format_meter method has multiple return statements:

  • return l_bar[:-1] + r_bar[1:]
  • return nobar # no `{bar}`; nothing else to do
  • return disp_trim(res, ncols) if ncols else res
  • return nobar
  • return disp_trim(res, ncols) if ncols else res
  • return (f'{(prefix + ": ") if prefix else ""}'

2 of them use disp_trim and 4 of them do not.

Problem statement and use case

I often use bar_format="{desc}" together with the set_description_str method to create status bars. Sometimes the desc is wider than the terminal, but it does not seem to be affected by ncols or dynamic_ncols.

Proposed solution

Wrap ALL return values of format_meter in disp_trim.

My current workaround

tqdm_bar = tqdm(bar_format="{desc}", dynamic_ncols=True, desc="...")
# Monkeypatch the `format_meter` method of the `tqdm` object.
original_format_meter = tqdm_bar.format_meter
@wraps(original_format_meter)
def format_meter(*args: Any, **kwargs: Any) -> str:
    result = original_format_meter(*args, **kwargs)
    return disp_trim(result, tqdm_bar.ncols or 10)
tqdm_bar.format_meter = format_meter  # type: ignore[method-assign]
with tqdm_bar:
    # Do lots of processing here, and update the description from time to time.
    ...

Read the original on github.com ↗