Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
Last active May 13, 2025 13:05
Show Gist options
  • Save vadimkantorov/468b752a7fe62f9041e961142998c762 to your computer and use it in GitHub Desktop.
Save vadimkantorov/468b752a7fe62f9041e961142998c762 to your computer and use it in GitHub Desktop.
Extremely simplified single-file, 20 LOC version of https://tqdm.github.io/docs/tqdm/ for debugging tqdm bugs like https://github.com/tqdm/tqdm/issues/760 or dropping the full dependency
# Save as tqdm.py in project dir, then `from tqdm import tqdm; from tqdm.auto import tqdm` should pick up this class, if fails use export PYTHONPATH=.
# Test run: python tqdm.py
import os, sys
# huggingface_hub/hf_api.py:
# from tqdm.auto import tqdm as base_tqdm
# from tqdm.contrib.concurrent import thread_map
# https://tqdm.github.io/docs/shortcuts/#tqdmauto
sys.modules['tqdm.auto'] = sys.modules[__name__]
# https://tqdm.github.io/docs/contrib.concurrent/#thread_map
sys.modules['tqdm.contrib.concurrent'] = sys.modules[__name__]
# https://github.com/tqdm/tqdm/blob/0ed5d7f18fa3153834cbac0aa57e8092b217cc16/tqdm/contrib/concurrent.py#L54
# TODO: mention tqdm's license
#def thread_map(*args, **kwargs):
# pass #TODO:
def trange(*args, **kwargs):
return tqdm(range(*args), **kwargs)
class tqdm:
def __init__(self, iterable, desc="", disable=False, total=None, initial=1, dynamic_ncols=True, ncols=80, nrows=80, postfix=None, **kwargs):
self.iterable = iterable
self.desc = desc
self.disable = disable or (os.getenv('TQDM_DISABLE') == '1')
self.postfix = postfix or {}
self.total = total
self.initial = initial
self.n = 1
self.format_dict = {}
def __iter__(self):
cnt = self.total if self.total is not None else len(self.iterable) if hasattr(self.iterable, '__len__') else 0
self.n = self.initial
for x in self.iterable:
if not self.disable:
print('\r', self.n, '/', cnt, ':', self.desc, '|', str(self.postfix), end='', flush=True)
self.n += 1
yield x
self.close()
def close(self):
if not self.disable:
print(flush=True)
def update(self, n : int | float = 1):
self.n = n
return False
def refresh(self, *args, **kwargs):
pass
def set_description(self, desc=None, refresh=True):
self.desc = desc or ''
def set_description_str(self, desc=None, refresh=True):
self.desc = desc or ''
def set_postfix(self, ordered_dict=None, **kwargs):
self.postfix = ordered_dict or {}
if __name__ == '__main__':
import time
from tqdm import tqdm
pbar = tqdm(list(range(50)), desc = 'hello', disable = False)
for i, x in enumerate(pbar):
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment