METADATA 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. Metadata-Version: 2.1
  2. Name: tqdm
  3. Version: 4.48.2
  4. Summary: Fast, Extensible Progress Meter
  5. Home-page: https://github.com/tqdm/tqdm
  6. Maintainer: tqdm developers
  7. Maintainer-email: python.tqdm@gmail.com
  8. License: MPLv2.0, MIT Licences
  9. Keywords: progressbar progressmeter progress bar meter rate eta console terminal time
  10. Platform: any
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Environment :: Console
  13. Classifier: Environment :: MacOS X
  14. Classifier: Environment :: Other Environment
  15. Classifier: Environment :: Win32 (MS Windows)
  16. Classifier: Environment :: X11 Applications
  17. Classifier: Framework :: IPython
  18. Classifier: Framework :: Jupyter
  19. Classifier: Intended Audience :: Developers
  20. Classifier: Intended Audience :: Education
  21. Classifier: Intended Audience :: End Users/Desktop
  22. Classifier: Intended Audience :: Other Audience
  23. Classifier: Intended Audience :: System Administrators
  24. Classifier: License :: OSI Approved :: MIT License
  25. Classifier: License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)
  26. Classifier: Operating System :: MacOS
  27. Classifier: Operating System :: MacOS :: MacOS X
  28. Classifier: Operating System :: Microsoft
  29. Classifier: Operating System :: Microsoft :: MS-DOS
  30. Classifier: Operating System :: Microsoft :: Windows
  31. Classifier: Operating System :: POSIX
  32. Classifier: Operating System :: POSIX :: BSD
  33. Classifier: Operating System :: POSIX :: BSD :: FreeBSD
  34. Classifier: Operating System :: POSIX :: Linux
  35. Classifier: Operating System :: POSIX :: SunOS/Solaris
  36. Classifier: Operating System :: Unix
  37. Classifier: Programming Language :: Python
  38. Classifier: Programming Language :: Python :: 2
  39. Classifier: Programming Language :: Python :: 2.6
  40. Classifier: Programming Language :: Python :: 2.7
  41. Classifier: Programming Language :: Python :: 3
  42. Classifier: Programming Language :: Python :: 3.2
  43. Classifier: Programming Language :: Python :: 3.3
  44. Classifier: Programming Language :: Python :: 3.4
  45. Classifier: Programming Language :: Python :: 3.5
  46. Classifier: Programming Language :: Python :: 3.6
  47. Classifier: Programming Language :: Python :: 3.7
  48. Classifier: Programming Language :: Python :: 3.8
  49. Classifier: Programming Language :: Python :: Implementation
  50. Classifier: Programming Language :: Python :: Implementation :: IronPython
  51. Classifier: Programming Language :: Python :: Implementation :: PyPy
  52. Classifier: Programming Language :: Unix Shell
  53. Classifier: Topic :: Desktop Environment
  54. Classifier: Topic :: Education :: Computer Aided Instruction (CAI)
  55. Classifier: Topic :: Education :: Testing
  56. Classifier: Topic :: Office/Business
  57. Classifier: Topic :: Other/Nonlisted Topic
  58. Classifier: Topic :: Software Development :: Build Tools
  59. Classifier: Topic :: Software Development :: Libraries
  60. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  61. Classifier: Topic :: Software Development :: Pre-processors
  62. Classifier: Topic :: Software Development :: User Interfaces
  63. Classifier: Topic :: System :: Installation/Setup
  64. Classifier: Topic :: System :: Logging
  65. Classifier: Topic :: System :: Monitoring
  66. Classifier: Topic :: System :: Shells
  67. Classifier: Topic :: Terminals
  68. Classifier: Topic :: Utilities
  69. Provides: tqdm
  70. Requires-Python: >=2.6, !=3.0.*, !=3.1.*
  71. Provides-Extra: dev
  72. Requires-Dist: py-make (>=0.1.0) ; extra == 'dev'
  73. Requires-Dist: twine ; extra == 'dev'
  74. Requires-Dist: argopt ; extra == 'dev'
  75. Requires-Dist: pydoc-markdown ; extra == 'dev'
  76. |Logo|
  77. tqdm
  78. ====
  79. |Py-Versions| |Versions| |Conda-Forge-Status| |Docker| |Snapcraft|
  80. |Build-Status| |Coverage-Status| |Branch-Coverage-Status| |Codacy-Grade| |Libraries-Rank| |PyPI-Downloads|
  81. |LICENCE| |OpenHub-Status| |binder-demo| |notebook-demo| |awesome-python|
  82. ``tqdm`` derives from the Arabic word *taqaddum* (تقدّم) which can mean "progress,"
  83. and is an abbreviation for "I love you so much" in Spanish (*te quiero demasiado*).
  84. Instantly make your loops show a smart progress meter - just wrap any
  85. iterable with ``tqdm(iterable)``, and you're done!
  86. .. code:: python
  87. from tqdm import tqdm
  88. for i in tqdm(range(10000)):
  89. ...
  90. ``76%|████████████████████████        | 7568/10000 [00:33<00:10, 229.00it/s]``
  91. ``trange(N)`` can be also used as a convenient shortcut for
  92. ``tqdm(range(N))``.
  93. |Screenshot|
  94. |Video| |Slides|
  95. It can also be executed as a module with pipes:
  96. .. code:: sh
  97. $ seq 9999999 | tqdm --bytes | wc -l
  98. 75.2MB [00:00, 217MB/s]
  99. 9999999
  100. $ tar -zcf - docs/ | tqdm --bytes --total `du -sb docs/ | cut -f1` \
  101. > backup.tgz
  102. 32%|██████████▍ | 8.89G/27.9G [00:42<01:31, 223MB/s]
  103. Overhead is low -- about 60ns per iteration (80ns with ``tqdm.gui``), and is
  104. unit tested against performance regression.
  105. By comparison, the well-established
  106. `ProgressBar <https://github.com/niltonvolpato/python-progressbar>`__ has
  107. an 800ns/iter overhead.
  108. In addition to its low overhead, ``tqdm`` uses smart algorithms to predict
  109. the remaining time and to skip unnecessary iteration displays, which allows
  110. for a negligible overhead in most cases.
  111. ``tqdm`` works on any platform
  112. (Linux, Windows, Mac, FreeBSD, NetBSD, Solaris/SunOS),
  113. in any console or in a GUI, and is also friendly with IPython/Jupyter notebooks.
  114. ``tqdm`` does not require any dependencies (not even ``curses``!), just
  115. Python and an environment supporting ``carriage return \r`` and
  116. ``line feed \n`` control characters.
  117. ------------------------------------------
  118. .. contents:: Table of contents
  119. :backlinks: top
  120. :local:
  121. Installation
  122. ------------
  123. Latest PyPI stable release
  124. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  125. |Versions| |PyPI-Downloads| |Libraries-Dependents|
  126. .. code:: sh
  127. pip install tqdm
  128. Latest development release on GitHub
  129. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  130. |GitHub-Status| |GitHub-Stars| |GitHub-Commits| |GitHub-Forks| |GitHub-Updated|
  131. Pull and install in the current directory:
  132. .. code:: sh
  133. pip install -e git+https://github.com/tqdm/tqdm.git@master#egg=tqdm
  134. Latest Conda release
  135. ~~~~~~~~~~~~~~~~~~~~
  136. |Conda-Forge-Status|
  137. .. code:: sh
  138. conda install -c conda-forge tqdm
  139. Latest Snapcraft release
  140. ~~~~~~~~~~~~~~~~~~~~~~~~
  141. |Snapcraft|
  142. There are 3 channels to choose from:
  143. .. code:: sh
  144. snap install tqdm # implies --stable, i.e. latest tagged release
  145. snap install tqdm --candidate # master branch
  146. snap install tqdm --edge # devel branch
  147. Note than ``snap`` binaries are purely for CLI use (not ``import``-able), and
  148. automatically set up ``bash`` tab-completion.
  149. Latest Docker release
  150. ~~~~~~~~~~~~~~~~~~~~~
  151. |Docker|
  152. .. code:: sh
  153. docker pull tqdm/tqdm
  154. docker run -i --rm tqdm/tqdm --help
  155. Other
  156. ~~~~~
  157. There are other (unofficial) places where ``tqdm`` may be downloaded, particularly for CLI use:
  158. |Repology|
  159. .. |Repology| image:: https://repology.org/badge/tiny-repos/python:tqdm.svg
  160. :target: https://repology.org/project/python:tqdm/versions
  161. Changelog
  162. ---------
  163. The list of all changes is available either on GitHub's Releases:
  164. |GitHub-Status|, on the
  165. `wiki <https://github.com/tqdm/tqdm/wiki/Releases>`__, or on the
  166. `website <https://tqdm.github.io/releases/>`__.
  167. Usage
  168. -----
  169. ``tqdm`` is very versatile and can be used in a number of ways.
  170. The three main ones are given below.
  171. Iterable-based
  172. ~~~~~~~~~~~~~~
  173. Wrap ``tqdm()`` around any iterable:
  174. .. code:: python
  175. from tqdm import tqdm
  176. from time import sleep
  177. text = ""
  178. for char in tqdm(["a", "b", "c", "d"]):
  179. sleep(0.25)
  180. text = text + char
  181. ``trange(i)`` is a special optimised instance of ``tqdm(range(i))``:
  182. .. code:: python
  183. from tqdm import trange
  184. for i in trange(100):
  185. sleep(0.01)
  186. Instantiation outside of the loop allows for manual control over ``tqdm()``:
  187. .. code:: python
  188. pbar = tqdm(["a", "b", "c", "d"])
  189. for char in pbar:
  190. sleep(0.25)
  191. pbar.set_description("Processing %s" % char)
  192. Manual
  193. ~~~~~~
  194. Manual control of ``tqdm()`` updates using a ``with`` statement:
  195. .. code:: python
  196. with tqdm(total=100) as pbar:
  197. for i in range(10):
  198. sleep(0.1)
  199. pbar.update(10)
  200. If the optional variable ``total`` (or an iterable with ``len()``) is
  201. provided, predictive stats are displayed.
  202. ``with`` is also optional (you can just assign ``tqdm()`` to a variable,
  203. but in this case don't forget to ``del`` or ``close()`` at the end:
  204. .. code:: python
  205. pbar = tqdm(total=100)
  206. for i in range(10):
  207. sleep(0.1)
  208. pbar.update(10)
  209. pbar.close()
  210. Module
  211. ~~~~~~
  212. Perhaps the most wonderful use of ``tqdm`` is in a script or on the command
  213. line. Simply inserting ``tqdm`` (or ``python -m tqdm``) between pipes will pass
  214. through all ``stdin`` to ``stdout`` while printing progress to ``stderr``.
  215. The example below demonstrated counting the number of lines in all Python files
  216. in the current directory, with timing information included.
  217. .. code:: sh
  218. $ time find . -name '*.py' -type f -exec cat \{} \; | wc -l
  219. 857365
  220. real 0m3.458s
  221. user 0m0.274s
  222. sys 0m3.325s
  223. $ time find . -name '*.py' -type f -exec cat \{} \; | tqdm | wc -l
  224. 857366it [00:03, 246471.31it/s]
  225. 857365
  226. real 0m3.585s
  227. user 0m0.862s
  228. sys 0m3.358s
  229. Note that the usual arguments for ``tqdm`` can also be specified.
  230. .. code:: sh
  231. $ find . -name '*.py' -type f -exec cat \{} \; |
  232. tqdm --unit loc --unit_scale --total 857366 >> /dev/null
  233. 100%|█████████████████████████████████| 857K/857K [00:04<00:00, 246Kloc/s]
  234. Backing up a large directory?
  235. .. code:: sh
  236. tar -zcf - docs/ | tqdm --bytes --total `du -sb docs/ | cut -f1` \
  237. > backup.tgz
  238. 44%|██████████████▊ | 153M/352M [00:14<00:18, 11.0MB/s]
  239. This can be beautified further:
  240. .. code:: sh
  241. BYTES="$(du -sb docs/ | cut -f1)"
  242. tar -cf - docs/ \
  243. | tqdm --bytes --total "$BYTES" --desc Processing | gzip \
  244. | tqdm --bytes --total "$BYTES" --desc Compressed --position 1 \
  245. > ~/backup.tgz
  246. Processing: 100%|██████████████████████| 352M/352M [00:14<00:00, 30.2MB/s]
  247. Compressed: 42%|█████████▎ | 148M/352M [00:14<00:19, 10.9MB/s]
  248. Or done on a file level using 7-zip:
  249. .. code:: sh
  250. 7z a -bd -r backup.7z docs/ | grep Compressing \
  251. | tqdm --total $(find docs/ -type f | wc -l) --unit files \
  252. | grep -v Compressing
  253. 100%|██████████████████████████▉| 15327/15327 [01:00<00:00, 712.96files/s]
  254. FAQ and Known Issues
  255. --------------------
  256. |GitHub-Issues|
  257. The most common issues relate to excessive output on multiple lines, instead
  258. of a neat one-line progress bar.
  259. - Consoles in general: require support for carriage return (``CR``, ``\r``).
  260. - Nested progress bars:
  261. * Consoles in general: require support for moving cursors up to the
  262. previous line. For example,
  263. `IDLE <https://github.com/tqdm/tqdm/issues/191#issuecomment-230168030>`__,
  264. `ConEmu <https://github.com/tqdm/tqdm/issues/254>`__ and
  265. `PyCharm <https://github.com/tqdm/tqdm/issues/203>`__ (also
  266. `here <https://github.com/tqdm/tqdm/issues/208>`__,
  267. `here <https://github.com/tqdm/tqdm/issues/307>`__, and
  268. `here <https://github.com/tqdm/tqdm/issues/454#issuecomment-335416815>`__)
  269. lack full support.
  270. * Windows: additionally may require the Python module ``colorama``
  271. to ensure nested bars stay within their respective lines.
  272. - Unicode:
  273. * Environments which report that they support unicode will have solid smooth
  274. progressbars. The fallback is an ```ascii``-only bar.
  275. * Windows consoles often only partially support unicode and thus
  276. `often require explicit ascii=True <https://github.com/tqdm/tqdm/issues/454#issuecomment-335416815>`__
  277. (also `here <https://github.com/tqdm/tqdm/issues/499>`__). This is due to
  278. either normal-width unicode characters being incorrectly displayed as
  279. "wide", or some unicode characters not rendering.
  280. - Wrapping generators:
  281. * Generator wrapper functions tend to hide the length of iterables.
  282. ``tqdm`` does not.
  283. * Replace ``tqdm(enumerate(...))`` with ``enumerate(tqdm(...))`` or
  284. ``tqdm(enumerate(x), total=len(x), ...)``.
  285. The same applies to ``numpy.ndenumerate``.
  286. * Replace ``tqdm(zip(a, b))`` with ``zip(tqdm(a), b)`` or even
  287. ``zip(tqdm(a), tqdm(b))``.
  288. * The same applies to ``itertools``.
  289. * Some useful convenience functions can be found under ``tqdm.contrib``.
  290. - `Hanging pipes in python2 <https://github.com/tqdm/tqdm/issues/359>`__:
  291. when using ``tqdm`` on the CLI, you may need to use Python 3.5+ for correct
  292. buffering.
  293. - `No intermediate output in docker-compose <https://github.com/tqdm/tqdm/issues/771>`__:
  294. use ``docker-compose run`` instead of ``docker-compose up`` and ``tty: true``.
  295. If you come across any other difficulties, browse and file |GitHub-Issues|.
  296. Documentation
  297. -------------
  298. |Py-Versions| |README-Hits| (Since 19 May 2016)
  299. .. code:: python
  300. class tqdm():
  301. """
  302. Decorate an iterable object, returning an iterator which acts exactly
  303. like the original iterable, but prints a dynamically updating
  304. progressbar every time a value is requested.
  305. """
  306. def __init__(self, iterable=None, desc=None, total=None, leave=True,
  307. file=None, ncols=None, mininterval=0.1,
  308. maxinterval=10.0, miniters=None, ascii=None, disable=False,
  309. unit='it', unit_scale=False, dynamic_ncols=False,
  310. smoothing=0.3, bar_format=None, initial=0, position=None,
  311. postfix=None, unit_divisor=1000):
  312. Parameters
  313. ~~~~~~~~~~
  314. * iterable : iterable, optional
  315. Iterable to decorate with a progressbar.
  316. Leave blank to manually manage the updates.
  317. * desc : str, optional
  318. Prefix for the progressbar.
  319. * total : int or float, optional
  320. The number of expected iterations. If unspecified,
  321. len(iterable) is used if possible. If float("inf") or as a last
  322. resort, only basic progress statistics are displayed
  323. (no ETA, no progressbar).
  324. If ``gui`` is True and this parameter needs subsequent updating,
  325. specify an initial arbitrary large positive number,
  326. e.g. 9e9.
  327. * leave : bool, optional
  328. If [default: True], keeps all traces of the progressbar
  329. upon termination of iteration.
  330. If ``None``, will leave only if ``position`` is ``0``.
  331. * file : ``io.TextIOWrapper`` or ``io.StringIO``, optional
  332. Specifies where to output the progress messages
  333. (default: sys.stderr). Uses ``file.write(str)`` and ``file.flush()``
  334. methods. For encoding, see ``write_bytes``.
  335. * ncols : int, optional
  336. The width of the entire output message. If specified,
  337. dynamically resizes the progressbar to stay within this bound.
  338. If unspecified, attempts to use environment width. The
  339. fallback is a meter width of 10 and no limit for the counter and
  340. statistics. If 0, will not print any meter (only stats).
  341. * mininterval : float, optional
  342. Minimum progress display update interval [default: 0.1] seconds.
  343. * maxinterval : float, optional
  344. Maximum progress display update interval [default: 10] seconds.
  345. Automatically adjusts ``miniters`` to correspond to ``mininterval``
  346. after long display update lag. Only works if ``dynamic_miniters``
  347. or monitor thread is enabled.
  348. * miniters : int or float, optional
  349. Minimum progress display update interval, in iterations.
  350. If 0 and ``dynamic_miniters``, will automatically adjust to equal
  351. ``mininterval`` (more CPU efficient, good for tight loops).
  352. If > 0, will skip display of specified number of iterations.
  353. Tweak this and ``mininterval`` to get very efficient loops.
  354. If your progress is erratic with both fast and slow iterations
  355. (network, skipping items, etc) you should set miniters=1.
  356. * ascii : bool or str, optional
  357. If unspecified or False, use unicode (smooth blocks) to fill
  358. the meter. The fallback is to use ASCII characters " 123456789#".
  359. * disable : bool, optional
  360. Whether to disable the entire progressbar wrapper
  361. [default: False]. If set to None, disable on non-TTY.
  362. * unit : str, optional
  363. String that will be used to define the unit of each iteration
  364. [default: it].
  365. * unit_scale : bool or int or float, optional
  366. If 1 or True, the number of iterations will be reduced/scaled
  367. automatically and a metric prefix following the
  368. International System of Units standard will be added
  369. (kilo, mega, etc.) [default: False]. If any other non-zero
  370. number, will scale ``total`` and ``n``.
  371. * dynamic_ncols : bool, optional
  372. If set, constantly alters ``ncols`` and ``nrows`` to the
  373. environment (allowing for window resizes) [default: False].
  374. * smoothing : float, optional
  375. Exponential moving average smoothing factor for speed estimates
  376. (ignored in GUI mode). Ranges from 0 (average speed) to 1
  377. (current/instantaneous speed) [default: 0.3].
  378. * bar_format : str, optional
  379. Specify a custom bar string formatting. May impact performance.
  380. [default: '{l_bar}{bar}{r_bar}'], where
  381. l_bar='{desc}: {percentage:3.0f}%|' and
  382. r_bar='| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, '
  383. '{rate_fmt}{postfix}]'
  384. Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
  385. percentage, elapsed, elapsed_s, ncols, nrows, desc, unit,
  386. rate, rate_fmt, rate_noinv, rate_noinv_fmt,
  387. rate_inv, rate_inv_fmt, postfix, unit_divisor,
  388. remaining, remaining_s.
  389. Note that a trailing ": " is automatically removed after {desc}
  390. if the latter is empty.
  391. * initial : int or float, optional
  392. The initial counter value. Useful when restarting a progress
  393. bar [default: 0]. If using float, consider specifying ``{n:.3f}``
  394. or similar in ``bar_format``, or specifying ``unit_scale``.
  395. * position : int, optional
  396. Specify the line offset to print this bar (starting from 0)
  397. Automatic if unspecified.
  398. Useful to manage multiple bars at once (eg, from threads).
  399. * postfix : dict or ``*``, optional
  400. Specify additional stats to display at the end of the bar.
  401. Calls ``set_postfix(**postfix)`` if possible (dict).
  402. * unit_divisor : float, optional
  403. [default: 1000], ignored unless ``unit_scale`` is True.
  404. * write_bytes : bool, optional
  405. If (default: None) and ``file`` is unspecified,
  406. bytes will be written in Python 2. If ``True`` will also write
  407. bytes. In all other cases will default to unicode.
  408. * lock_args : tuple, optional
  409. Passed to ``refresh`` for intermediate output
  410. (initialisation, iterating, and updating).
  411. * nrows : int, optional
  412. The screen height. If specified, hides nested bars outside this
  413. bound. If unspecified, attempts to use environment height.
  414. The fallback is 20.
  415. Extra CLI Options
  416. ~~~~~~~~~~~~~~~~~
  417. * delim : chr, optional
  418. Delimiting character [default: '\n']. Use '\0' for null.
  419. N.B.: on Windows systems, Python converts '\n' to '\r\n'.
  420. * buf_size : int, optional
  421. String buffer size in bytes [default: 256]
  422. used when ``delim`` is specified.
  423. * bytes : bool, optional
  424. If true, will count bytes, ignore ``delim``, and default
  425. ``unit_scale`` to True, ``unit_divisor`` to 1024, and ``unit`` to 'B'.
  426. * manpath : str, optional
  427. Directory in which to install tqdm man pages.
  428. * comppath : str, optional
  429. Directory in which to place tqdm completion.
  430. * log : str, optional
  431. CRITICAL|FATAL|ERROR|WARN(ING)|[default: 'INFO']|DEBUG|NOTSET.
  432. Returns
  433. ~~~~~~~
  434. * out : decorated iterator.
  435. .. code:: python
  436. class tqdm():
  437. def update(self, n=1):
  438. """
  439. Manually update the progress bar, useful for streams
  440. such as reading files.
  441. E.g.:
  442. >>> t = tqdm(total=filesize) # Initialise
  443. >>> for current_buffer in stream:
  444. ... ...
  445. ... t.update(len(current_buffer))
  446. >>> t.close()
  447. The last line is highly recommended, but possibly not necessary if
  448. ``t.update()`` will be called in such a way that ``filesize`` will be
  449. exactly reached and printed.
  450. Parameters
  451. ----------
  452. n : int or float, optional
  453. Increment to add to the internal counter of iterations
  454. [default: 1]. If using float, consider specifying ``{n:.3f}``
  455. or similar in ``bar_format``, or specifying ``unit_scale``.
  456. """
  457. def close(self):
  458. """Cleanup and (if leave=False) close the progressbar."""
  459. def clear(self, nomove=False):
  460. """Clear current bar display."""
  461. def refresh(self):
  462. """
  463. Force refresh the display of this bar.
  464. Parameters
  465. ----------
  466. nolock : bool, optional
  467. If ``True``, does not lock.
  468. If [default: ``False``]: calls ``acquire()`` on internal lock.
  469. lock_args : tuple, optional
  470. Passed to internal lock's ``acquire()``.
  471. If specified, will only ``display()`` if ``acquire()`` returns ``True``.
  472. """
  473. def unpause(self):
  474. """Restart tqdm timer from last print time."""
  475. def reset(self, total=None):
  476. """
  477. Resets to 0 iterations for repeated use.
  478. Consider combining with ``leave=True``.
  479. Parameters
  480. ----------
  481. total : int or float, optional. Total to use for the new bar.
  482. """
  483. def set_description(self, desc=None, refresh=True):
  484. """
  485. Set/modify description of the progress bar.
  486. Parameters
  487. ----------
  488. desc : str, optional
  489. refresh : bool, optional
  490. Forces refresh [default: True].
  491. """
  492. def set_postfix(self, ordered_dict=None, refresh=True, **tqdm_kwargs):
  493. """
  494. Set/modify postfix (additional stats)
  495. with automatic formatting based on datatype.
  496. Parameters
  497. ----------
  498. ordered_dict : dict or OrderedDict, optional
  499. refresh : bool, optional
  500. Forces refresh [default: True].
  501. kwargs : dict, optional
  502. """
  503. @classmethod
  504. def write(cls, s, file=sys.stdout, end="\n"):
  505. """Print a message via tqdm (without overlap with bars)."""
  506. @property
  507. def format_dict(self):
  508. """Public API for read-only member access."""
  509. def display(self, msg=None, pos=None):
  510. """
  511. Use ``self.sp`` to display ``msg`` in the specified ``pos``.
  512. Consider overloading this function when inheriting to use e.g.:
  513. ``self.some_frontend(**self.format_dict)`` instead of ``self.sp``.
  514. Parameters
  515. ----------
  516. msg : str, optional. What to display (default: ``repr(self)``).
  517. pos : int, optional. Position to ``moveto``
  518. (default: ``abs(self.pos)``).
  519. """
  520. @classmethod
  521. @contextmanager
  522. def wrapattr(cls, stream, method, total=None, bytes=True, **tqdm_kwargs):
  523. """
  524. stream : file-like object.
  525. method : str, "read" or "write". The result of ``read()`` and
  526. the first argument of ``write()`` should have a ``len()``.
  527. >>> with tqdm.wrapattr(file_obj, "read", total=file_obj.size) as fobj:
  528. ... while True:
  529. ... chunk = fobj.read(chunk_size)
  530. ... if not chunk:
  531. ... break
  532. """
  533. @classmethod
  534. def pandas(cls, *targs, **tqdm_kwargs):
  535. """Registers the current `tqdm` class with `pandas`."""
  536. def trange(*args, **tqdm_kwargs):
  537. """
  538. A shortcut for `tqdm(xrange(*args), **tqdm_kwargs)`.
  539. On Python3+, `range` is used instead of `xrange`.
  540. """
  541. class tqdm.notebook.tqdm(tqdm.tqdm):
  542. """Experimental IPython/Jupyter Notebook widget."""
  543. def tqdm.notebook.trange(*args, **tqdm_kwargs):
  544. """Experimental IPython/Jupyter Notebook widget version of `trange`."""
  545. class tqdm.auto.tqdm(tqdm.tqdm):
  546. """Automatically chooses beween `tqdm.notebook` and `tqdm.tqdm`."""
  547. class tqdm.asyncio.tqdm(tqdm.tqdm):
  548. """Asynchronous version."""
  549. @classmethod
  550. def as_completed(cls, fs, *, loop=None, timeout=None, total=None,
  551. **tqdm_kwargs):
  552. """Wrapper for `asyncio.as_completed`."""
  553. class tqdm.gui.tqdm(tqdm.tqdm):
  554. """Experimental GUI version."""
  555. class tqdm.keras.TqdmCallback(keras.callbacks.Callback):
  556. """`keras` callback for epoch and batch progress."""
  557. def tqdm.contrib.tenumerate(iterable, start=0, total=None,
  558. tqdm_class=tqdm.auto.tqdm, **tqdm_kwargs):
  559. """Equivalent of `numpy.ndenumerate` or builtin `enumerate`."""
  560. def tqdm.contrib.tzip(iter1, *iter2plus, **tqdm_kwargs):
  561. """Equivalent of builtin `zip`."""
  562. def tqdm.contrib.tmap(function, *sequences, **tqdm_kwargs):
  563. """Equivalent of builtin `map`."""
  564. ``contrib``
  565. -----------
  566. The ``tqdm.contrib`` package also contains experimental modules:
  567. - ``tqdm.contrib.itertools``: Thin wrappers around ``itertools``
  568. - ``tqdm.contrib.concurrent``: Thin wrappers around ``concurrent.futures``
  569. - ``tqdm.contrib.discord``: Posts to `Discord <https://discord.com/>`__ bots
  570. - ``tqdm.contrib.telegram``: Posts to `Telegram <https://telegram.org/>`__ bots
  571. - ``tqdm.contrib.bells``: Automagically enables all optional features
  572. * ``auto``, ``pandas``, ``discord``, ``telegram``
  573. Examples and Advanced Usage
  574. ---------------------------
  575. - See the `examples <https://github.com/tqdm/tqdm/tree/master/examples>`__
  576. folder;
  577. - import the module and run ``help()``;
  578. - consult the `wiki <https://github.com/tqdm/tqdm/wiki>`__;
  579. * this has an
  580. `excellent article <https://github.com/tqdm/tqdm/wiki/How-to-make-a-great-Progress-Bar>`__
  581. on how to make a **great** progressbar;
  582. - check out the `slides from PyData London <https://tqdm.github.io/PyData2019/slides.html>`__, or
  583. - run the |notebook-demo| or |binder-demo|.
  584. Description and additional stats
  585. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  586. Custom information can be displayed and updated dynamically on ``tqdm`` bars
  587. with the ``desc`` and ``postfix`` arguments:
  588. .. code:: python
  589. from tqdm import tqdm, trange
  590. from random import random, randint
  591. from time import sleep
  592. with trange(10) as t:
  593. for i in t:
  594. # Description will be displayed on the left
  595. t.set_description('GEN %i' % i)
  596. # Postfix will be displayed on the right,
  597. # formatted automatically based on argument's datatype
  598. t.set_postfix(loss=random(), gen=randint(1,999), str='h',
  599. lst=[1, 2])
  600. sleep(0.1)
  601. with tqdm(total=10, bar_format="{postfix[0]} {postfix[1][value]:>8.2g}",
  602. postfix=["Batch", dict(value=0)]) as t:
  603. for i in range(10):
  604. sleep(0.1)
  605. t.postfix[1]["value"] = i / 2
  606. t.update()
  607. Points to remember when using ``{postfix[...]}`` in the ``bar_format`` string:
  608. - ``postfix`` also needs to be passed as an initial argument in a compatible
  609. format, and
  610. - ``postfix`` will be auto-converted to a string if it is a ``dict``-like
  611. object. To prevent this behaviour, insert an extra item into the dictionary
  612. where the key is not a string.
  613. Additional ``bar_format`` parameters may also be defined by overriding
  614. ``format_dict``, and the bar itself may be modified using ``ascii``:
  615. .. code:: python
  616. from tqdm import tqdm
  617. class TqdmExtraFormat(tqdm):
  618. """Provides a `total_time` format parameter"""
  619. @property
  620. def format_dict(self):
  621. d = super(TqdmExtraFormat, self).format_dict
  622. total_time = d["elapsed"] * (d["total"] or 0) / max(d["n"], 1)
  623. d.update(total_time=self.format_interval(total_time) + " in total")
  624. return d
  625. for i in TqdmExtraFormat(
  626. range(9), ascii=" .oO0",
  627. bar_format="{total_time}: {percentage:.0f}%|{bar}{r_bar}"):
  628. if i == 4:
  629. break
  630. .. code::
  631. 00:00 in total: 44%|0000. | 4/9 [00:00<00:00, 962.93it/s]
  632. Note that ``{bar}`` also supports a format specifier ``[width][type]``.
  633. - ``width``
  634. * unspecified (default): automatic to fill ``ncols``
  635. * ``int >= 0``: fixed width overriding ``ncols`` logic
  636. * ``int < 0``: subtract from the automatic default
  637. - ``type``
  638. * ``a``: ascii (``ascii=True`` override)
  639. * ``u``: unicode (``ascii=False`` override)
  640. * ``b``: blank (``ascii=" "`` override)
  641. This means a fixed bar with right-justified text may be created by using:
  642. ``bar_format="{l_bar}{bar:10}|{bar:-10b}right-justified"``
  643. Nested progress bars
  644. ~~~~~~~~~~~~~~~~~~~~
  645. ``tqdm`` supports nested progress bars. Here's an example:
  646. .. code:: python
  647. from tqdm.auto import trange
  648. from time import sleep
  649. for i in trange(4, desc='1st loop'):
  650. for j in trange(5, desc='2nd loop'):
  651. for k in trange(50, desc='3rd loop', leave=False):
  652. sleep(0.01)
  653. On Windows `colorama <https://github.com/tartley/colorama>`__ will be used if
  654. available to keep nested bars on their respective lines.
  655. For manual control over positioning (e.g. for multi-processing use),
  656. you may specify ``position=n`` where ``n=0`` for the outermost bar,
  657. ``n=1`` for the next, and so on.
  658. However, it's best to check if `tqdm` can work without manual `position` first.
  659. .. code:: python
  660. from time import sleep
  661. from tqdm import trange, tqdm
  662. from multiprocessing import Pool, freeze_support
  663. L = list(range(9))
  664. def progresser(n):
  665. interval = 0.001 / (n + 2)
  666. total = 5000
  667. text = "#{}, est. {:<04.2}s".format(n, interval * total)
  668. for _ in trange(total, desc=text, position=n):
  669. sleep(interval)
  670. if __name__ == '__main__':
  671. freeze_support() # for Windows support
  672. p = Pool(initializer=tqdm.set_lock, initargs=(tqdm.get_lock(),))
  673. p.map(progresser, L)
  674. Note that in Python 3, ``tqdm.write`` is thread-safe:
  675. .. code:: python
  676. from time import sleep
  677. from tqdm import tqdm, trange
  678. from concurrent.futures import ThreadPoolExecutor
  679. L = list(range(9))
  680. def progresser(n):
  681. interval = 0.001 / (n + 2)
  682. total = 5000
  683. text = "#{}, est. {:<04.2}s".format(n, interval * total)
  684. for _ in trange(total, desc=text):
  685. sleep(interval)
  686. if n == 6:
  687. tqdm.write("n == 6 completed.")
  688. tqdm.write("`tqdm.write()` is thread-safe in py3!")
  689. if __name__ == '__main__':
  690. with ThreadPoolExecutor() as p:
  691. p.map(progresser, L)
  692. Hooks and callbacks
  693. ~~~~~~~~~~~~~~~~~~~
  694. ``tqdm`` can easily support callbacks/hooks and manual updates.
  695. Here's an example with ``urllib``:
  696. **``urllib.urlretrieve`` documentation**
  697. | [...]
  698. | If present, the hook function will be called once
  699. | on establishment of the network connection and once after each block read
  700. | thereafter. The hook will be passed three arguments; a count of blocks
  701. | transferred so far, a block size in bytes, and the total size of the file.
  702. | [...]
  703. .. code:: python
  704. import urllib, os
  705. from tqdm import tqdm
  706. class TqdmUpTo(tqdm):
  707. """Provides `update_to(n)` which uses `tqdm.update(delta_n)`."""
  708. def update_to(self, b=1, bsize=1, tsize=None):
  709. """
  710. b : int, optional
  711. Number of blocks transferred so far [default: 1].
  712. bsize : int, optional
  713. Size of each block (in tqdm units) [default: 1].
  714. tsize : int, optional
  715. Total size (in tqdm units). If [default: None] remains unchanged.
  716. """
  717. if tsize is not None:
  718. self.total = tsize
  719. self.update(b * bsize - self.n) # will also set self.n = b * bsize
  720. eg_link = "https://caspersci.uk.to/matryoshka.zip"
  721. with TqdmUpTo(unit='B', unit_scale=True, unit_divisor=1024, miniters=1,
  722. desc=eg_link.split('/')[-1]) as t: # all optional kwargs
  723. urllib.urlretrieve(eg_link, filename=os.devnull,
  724. reporthook=t.update_to, data=None)
  725. t.total = t.n
  726. Inspired by `twine#242 <https://github.com/pypa/twine/pull/242>`__.
  727. Functional alternative in
  728. `examples/tqdm_wget.py <https://github.com/tqdm/tqdm/blob/master/examples/tqdm_wget.py>`__.
  729. It is recommend to use ``miniters=1`` whenever there is potentially
  730. large differences in iteration speed (e.g. downloading a file over
  731. a patchy connection).
  732. **Wrapping read/write methods**
  733. To measure throughput through a file-like object's ``read`` or ``write``
  734. methods, use ``CallbackIOWrapper``:
  735. .. code:: python
  736. from tqdm.auto import tqdm
  737. from tqdm.utils import CallbackIOWrapper
  738. with tqdm(total=file_obj.size,
  739. unit='B', unit_scale=True, unit_divisor=1024) as t:
  740. fobj = CallbackIOWrapper(t.update, file_obj, "read")
  741. while True:
  742. chunk = fobj.read(chunk_size)
  743. if not chunk:
  744. break
  745. t.reset()
  746. # ... continue to use `t` for something else
  747. Alternatively, use the even simpler ``wrapattr`` convenience function,
  748. which would condense both the ``urllib`` and ``CallbackIOWrapper`` examples
  749. down to:
  750. .. code:: python
  751. import urllib, os
  752. from tqdm import tqdm
  753. eg_link = "https://caspersci.uk.to/matryoshka.zip"
  754. with tqdm.wrapattr(open(os.devnull, "wb"), "write",
  755. miniters=1, desc=eg_link.split('/')[-1]) as fout:
  756. for chunk in urllib.urlopen(eg_link):
  757. fout.write(chunk)
  758. The ``requests`` equivalent is nearly identical, albeit with a ``total``:
  759. .. code:: python
  760. import requests, os
  761. from tqdm import tqdm
  762. eg_link = "https://caspersci.uk.to/matryoshka.zip"
  763. response = requests.get(eg_link, stream=True)
  764. with tqdm.wrapattr(open(os.devnull, "wb"), "write",
  765. miniters=1, desc=eg_link.split('/')[-1],
  766. total=int(response.headers.get('content-length', 0))) as fout:
  767. for chunk in response.iter_content(chunk_size=4096):
  768. fout.write(chunk)
  769. Pandas Integration
  770. ~~~~~~~~~~~~~~~~~~
  771. Due to popular demand we've added support for ``pandas`` -- here's an example
  772. for ``DataFrame.progress_apply`` and ``DataFrameGroupBy.progress_apply``:
  773. .. code:: python
  774. import pandas as pd
  775. import numpy as np
  776. from tqdm import tqdm
  777. df = pd.DataFrame(np.random.randint(0, 100, (100000, 6)))
  778. # Register `pandas.progress_apply` and `pandas.Series.map_apply` with `tqdm`
  779. # (can use `tqdm.gui.tqdm`, `tqdm.notebook.tqdm`, optional kwargs, etc.)
  780. tqdm.pandas(desc="my bar!")
  781. # Now you can use `progress_apply` instead of `apply`
  782. # and `progress_map` instead of `map`
  783. df.progress_apply(lambda x: x**2)
  784. # can also groupby:
  785. # df.groupby(0).progress_apply(lambda x: x**2)
  786. In case you're interested in how this works (and how to modify it for your
  787. own callbacks), see the
  788. `examples <https://github.com/tqdm/tqdm/tree/master/examples>`__
  789. folder or import the module and run ``help()``.
  790. Keras Integration
  791. ~~~~~~~~~~~~~~~~~
  792. A ``keras`` callback is also available:
  793. .. code:: python
  794. from tqdm.keras import TqdmCallback
  795. ...
  796. model.fit(..., verbose=0, callbacks=[TqdmCallback()])
  797. IPython/Jupyter Integration
  798. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  799. IPython/Jupyter is supported via the ``tqdm.notebook`` submodule:
  800. .. code:: python
  801. from tqdm.notebook import trange, tqdm
  802. from time import sleep
  803. for i in trange(3, desc='1st loop'):
  804. for j in tqdm(range(100), desc='2nd loop'):
  805. sleep(0.01)
  806. In addition to ``tqdm`` features, the submodule provides a native Jupyter
  807. widget (compatible with IPython v1-v4 and Jupyter), fully working nested bars
  808. and colour hints (blue: normal, green: completed, red: error/interrupt,
  809. light blue: no ETA); as demonstrated below.
  810. |Screenshot-Jupyter1|
  811. |Screenshot-Jupyter2|
  812. |Screenshot-Jupyter3|
  813. The ``notebook`` version supports percentage or pixels for overall width
  814. (e.g.: ``ncols='100%'`` or ``ncols='480px'``).
  815. It is also possible to let ``tqdm`` automatically choose between
  816. console or notebook versions by using the ``autonotebook`` submodule:
  817. .. code:: python
  818. from tqdm.autonotebook import tqdm
  819. tqdm.pandas()
  820. Note that this will issue a ``TqdmExperimentalWarning`` if run in a notebook
  821. since it is not meant to be possible to distinguish between ``jupyter notebook``
  822. and ``jupyter console``. Use ``auto`` instead of ``autonotebook`` to suppress
  823. this warning.
  824. Note that notebooks will display the bar in the cell where it was created.
  825. This may be a different cell from the one where it is used.
  826. If this is not desired, the creation of the bar must be delayed/moved to the
  827. cell where it is desired to be displayed.
  828. Another possibility is to have a single bar (near the top of the notebook)
  829. which is constantly re-used (using ``reset()`` rather than ``close()``).
  830. For this reason, the notebook version (unlike the CLI version) does not
  831. automatically call ``close()`` upon ``Exception``.
  832. .. code:: python
  833. from tqdm.notebook import tqdm
  834. pbar = tqdm()
  835. .. code:: python
  836. # different cell
  837. iterable = range(100)
  838. pbar.reset(total=len(iterable)) # initialise with new `total`
  839. for i in iterable:
  840. pbar.update()
  841. pbar.refresh() # force print final status but don't `close()`
  842. Custom Integration
  843. ~~~~~~~~~~~~~~~~~~
  844. To change the default arguments (such as making ``dynamic_ncols=True``),
  845. simply use built-in Python magic:
  846. .. code:: python
  847. from functools import partial
  848. from tqdm import tqdm as std_tqdm
  849. tqdm = partial(std_tqdm, dynamic_ncols=True)
  850. For further customisation,
  851. ``tqdm`` may be inherited from to create custom callbacks (as with the
  852. ``TqdmUpTo`` example `above <#hooks-and-callbacks>`__) or for custom frontends
  853. (e.g. GUIs such as notebook or plotting packages). In the latter case:
  854. 1. ``def __init__()`` to call ``super().__init__(..., gui=True)`` to disable
  855. terminal ``status_printer`` creation.
  856. 2. Redefine: ``close()``, ``clear()``, ``display()``.
  857. Consider overloading ``display()`` to use e.g.
  858. ``self.frontend(**self.format_dict)`` instead of ``self.sp(repr(self))``.
  859. `tqdm/notebook.py <https://github.com/tqdm/tqdm/blob/master/tqdm/notebook.py>`__
  860. and `tqdm/gui.py <https://github.com/tqdm/tqdm/blob/master/tqdm/gui.py>`__
  861. submodules are examples of inheritance which don't (yet) strictly conform to the
  862. above recommendation.
  863. Dynamic Monitor/Meter
  864. ~~~~~~~~~~~~~~~~~~~~~
  865. You can use a ``tqdm`` as a meter which is not monotonically increasing.
  866. This could be because ``n`` decreases (e.g. a CPU usage monitor) or ``total``
  867. changes.
  868. One example would be recursively searching for files. The ``total`` is the
  869. number of objects found so far, while ``n`` is the number of those objects which
  870. are files (rather than folders):
  871. .. code:: python
  872. from tqdm import tqdm
  873. import os.path
  874. def find_files_recursively(path, show_progress=True):
  875. files = []
  876. # total=1 assumes `path` is a file
  877. t = tqdm(total=1, unit="file", disable=not show_progress)
  878. if not os.path.exists(path):
  879. raise IOError("Cannot find:" + path)
  880. def append_found_file(f):
  881. files.append(f)
  882. t.update()
  883. def list_found_dir(path):
  884. """returns os.listdir(path) assuming os.path.isdir(path)"""
  885. listing = os.listdir(path)
  886. # subtract 1 since a "file" we found was actually this directory
  887. t.total += len(listing) - 1
  888. # fancy way to give info without forcing a refresh
  889. t.set_postfix(dir=path[-10:], refresh=False)
  890. t.update(0) # may trigger a refresh
  891. return listing
  892. def recursively_search(path):
  893. if os.path.isdir(path):
  894. for f in list_found_dir(path):
  895. recursively_search(os.path.join(path, f))
  896. else:
  897. append_found_file(path)
  898. recursively_search(path)
  899. t.set_postfix(dir=path)
  900. t.close()
  901. return files
  902. Using ``update(0)`` is a handy way to let ``tqdm`` decide when to trigger a
  903. display refresh to avoid console spamming.
  904. Writing messages
  905. ~~~~~~~~~~~~~~~~
  906. This is a work in progress (see
  907. `#737 <https://github.com/tqdm/tqdm/issues/737>`__).
  908. Since ``tqdm`` uses a simple printing mechanism to display progress bars,
  909. you should not write any message in the terminal using ``print()`` while
  910. a progressbar is open.
  911. To write messages in the terminal without any collision with ``tqdm`` bar
  912. display, a ``.write()`` method is provided:
  913. .. code:: python
  914. from tqdm.auto import tqdm, trange
  915. from time import sleep
  916. bar = trange(10)
  917. for i in bar:
  918. # Print using tqdm class method .write()
  919. sleep(0.1)
  920. if not (i % 3):
  921. tqdm.write("Done task %i" % i)
  922. # Can also use bar.write()
  923. By default, this will print to standard output ``sys.stdout``. but you can
  924. specify any file-like object using the ``file`` argument. For example, this
  925. can be used to redirect the messages writing to a log file or class.
  926. Redirecting writing
  927. ~~~~~~~~~~~~~~~~~~~
  928. If using a library that can print messages to the console, editing the library
  929. by replacing ``print()`` with ``tqdm.write()`` may not be desirable.
  930. In that case, redirecting ``sys.stdout`` to ``tqdm.write()`` is an option.
  931. To redirect ``sys.stdout``, create a file-like class that will write
  932. any input string to ``tqdm.write()``, and supply the arguments
  933. ``file=sys.stdout, dynamic_ncols=True``.
  934. A reusable canonical example is given below:
  935. .. code:: python
  936. from time import sleep
  937. import contextlib
  938. import sys
  939. from tqdm import tqdm
  940. from tqdm.contrib import DummyTqdmFile
  941. @contextlib.contextmanager
  942. def std_out_err_redirect_tqdm():
  943. orig_out_err = sys.stdout, sys.stderr
  944. try:
  945. sys.stdout, sys.stderr = map(DummyTqdmFile, orig_out_err)
  946. yield orig_out_err[0]
  947. # Relay exceptions
  948. except Exception as exc:
  949. raise exc
  950. # Always restore sys.stdout/err if necessary
  951. finally:
  952. sys.stdout, sys.stderr = orig_out_err
  953. def some_fun(i):
  954. print("Fee, fi, fo,".split()[i])
  955. # Redirect stdout to tqdm.write() (don't forget the `as save_stdout`)
  956. with std_out_err_redirect_tqdm() as orig_stdout:
  957. # tqdm needs the original stdout
  958. # and dynamic_ncols=True to autodetect console width
  959. for i in tqdm(range(3), file=orig_stdout, dynamic_ncols=True):
  960. sleep(.5)
  961. some_fun(i)
  962. # After the `with`, printing is restored
  963. print("Done!")
  964. Monitoring thread, intervals and miniters
  965. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  966. ``tqdm`` implements a few tricks to to increase efficiency and reduce overhead.
  967. - Avoid unnecessary frequent bar refreshing: ``mininterval`` defines how long
  968. to wait between each refresh. ``tqdm`` always gets updated in the background,
  969. but it will display only every ``mininterval``.
  970. - Reduce number of calls to check system clock/time.
  971. - ``mininterval`` is more intuitive to configure than ``miniters``.
  972. A clever adjustment system ``dynamic_miniters`` will automatically adjust
  973. ``miniters`` to the amount of iterations that fit into time ``mininterval``.
  974. Essentially, ``tqdm`` will check if it's time to print without actually
  975. checking time. This behaviour can be still be bypassed by manually setting
  976. ``miniters``.
  977. However, consider a case with a combination of fast and slow iterations.
  978. After a few fast iterations, ``dynamic_miniters`` will set ``miniters`` to a
  979. large number. When iteration rate subsequently slows, ``miniters`` will
  980. remain large and thus reduce display update frequency. To address this:
  981. - ``maxinterval`` defines the maximum time between display refreshes.
  982. A concurrent monitoring thread checks for overdue updates and forces one
  983. where necessary.
  984. The monitoring thread should not have a noticeable overhead, and guarantees
  985. updates at least every 10 seconds by default.
  986. This value can be directly changed by setting the ``monitor_interval`` of
  987. any ``tqdm`` instance (i.e. ``t = tqdm.tqdm(...); t.monitor_interval = 2``).
  988. The monitor thread may be disabled application-wide by setting
  989. ``tqdm.tqdm.monitor_interval = 0`` before instantiation of any ``tqdm`` bar.
  990. Contributions
  991. -------------
  992. |GitHub-Commits| |GitHub-Issues| |GitHub-PRs| |OpenHub-Status| |GitHub-Contributions| |CII Best Practices|
  993. All source code is hosted on `GitHub <https://github.com/tqdm/tqdm>`__.
  994. Contributions are welcome.
  995. See the
  996. `CONTRIBUTING <https://raw.githubusercontent.com/tqdm/tqdm/master/CONTRIBUTING.md>`__
  997. file for more information.
  998. Developers who have made significant contributions, ranked by *SLoC*
  999. (surviving lines of code,
  1000. `git fame <https://github.com/casperdcl/git-fame>`__ ``-wMC``),
  1001. are:
  1002. ==================== ======================================================== ==== ================================
  1003. Name ID SLoC Notes
  1004. ==================== ======================================================== ==== ================================
  1005. Casper da Costa-Luis `casperdcl <https://github.com/casperdcl>`__ ~75% primary maintainer |Gift-Casper|
  1006. Stephen Larroque `lrq3000 <https://github.com/lrq3000>`__ ~15% team member
  1007. Martin Zugnoni `martinzugnoni <https://github.com/martinzugnoni>`__ ~3%
  1008. Guangshuo Chen `chengs <https://github.com/chengs>`__ ~1%
  1009. Hadrien Mary `hadim <https://github.com/hadim>`__ ~1% team member
  1010. Matthew Stevens `mjstevens777 <https://github.com/mjstevens777>`__ ~1%
  1011. Noam Yorav-Raphael `noamraph <https://github.com/noamraph>`__ ~1% original author
  1012. Kyle Altendorf `altendky <https://github.com/altendky>`__ ~1%
  1013. Ivan Ivanov `obiwanus <https://github.com/obiwanus>`__ ~1%
  1014. James E. King III `jeking3 <https://github.com/jeking3>`__ ~1%
  1015. Mikhail Korobov `kmike <https://github.com/kmike>`__ ~1% team member
  1016. ==================== ======================================================== ==== ================================
  1017. |sourcerer-0| |sourcerer-1| |sourcerer-2| |sourcerer-3| |sourcerer-4| |sourcerer-5| |sourcerer-7|
  1018. Ports to Other Languages
  1019. ~~~~~~~~~~~~~~~~~~~~~~~~
  1020. A list is available on
  1021. `this wiki page <https://github.com/tqdm/tqdm/wiki/tqdm-ports>`__.
  1022. LICENCE
  1023. -------
  1024. Open Source (OSI approved): |LICENCE|
  1025. Citation information: |DOI|
  1026. |README-Hits| (Since 19 May 2016)
  1027. .. |Logo| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/logo.gif
  1028. .. |Screenshot| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/tqdm.gif
  1029. .. |Video| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/video.jpg
  1030. :target: https://tqdm.github.io/video
  1031. .. |Slides| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/slides.jpg
  1032. :target: https://tqdm.github.io/PyData2019/slides.html
  1033. .. |Build-Status| image:: https://img.shields.io/travis/tqdm/tqdm/master.svg?logo=travis
  1034. :target: https://travis-ci.org/tqdm/tqdm
  1035. .. |Coverage-Status| image:: https://coveralls.io/repos/tqdm/tqdm/badge.svg?branch=master
  1036. :target: https://coveralls.io/github/tqdm/tqdm
  1037. .. |Branch-Coverage-Status| image:: https://codecov.io/gh/tqdm/tqdm/branch/master/graph/badge.svg
  1038. :target: https://codecov.io/gh/tqdm/tqdm
  1039. .. |Codacy-Grade| image:: https://api.codacy.com/project/badge/Grade/3f965571598f44549c7818f29cdcf177
  1040. :target: https://www.codacy.com/app/tqdm/tqdm/dashboard
  1041. .. |CII Best Practices| image:: https://bestpractices.coreinfrastructure.org/projects/3264/badge
  1042. :target: https://bestpractices.coreinfrastructure.org/projects/3264
  1043. .. |GitHub-Status| image:: https://img.shields.io/github/tag/tqdm/tqdm.svg?maxAge=86400&logo=github&logoColor=white
  1044. :target: https://github.com/tqdm/tqdm/releases
  1045. .. |GitHub-Forks| image:: https://img.shields.io/github/forks/tqdm/tqdm.svg?logo=github&logoColor=white
  1046. :target: https://github.com/tqdm/tqdm/network
  1047. .. |GitHub-Stars| image:: https://img.shields.io/github/stars/tqdm/tqdm.svg?logo=github&logoColor=white
  1048. :target: https://github.com/tqdm/tqdm/stargazers
  1049. .. |GitHub-Commits| image:: https://img.shields.io/github/commit-activity/y/tqdm/tqdm.svg?logo=git&logoColor=white
  1050. :target: https://github.com/tqdm/tqdm/graphs/commit-activity
  1051. .. |GitHub-Issues| image:: https://img.shields.io/github/issues-closed/tqdm/tqdm.svg?logo=github&logoColor=white
  1052. :target: https://github.com/tqdm/tqdm/issues?q=
  1053. .. |GitHub-PRs| image:: https://img.shields.io/github/issues-pr-closed/tqdm/tqdm.svg?logo=github&logoColor=white
  1054. :target: https://github.com/tqdm/tqdm/pulls
  1055. .. |GitHub-Contributions| image:: https://img.shields.io/github/contributors/tqdm/tqdm.svg?logo=github&logoColor=white
  1056. :target: https://github.com/tqdm/tqdm/graphs/contributors
  1057. .. |GitHub-Updated| image:: https://img.shields.io/github/last-commit/tqdm/tqdm/master.svg?logo=github&logoColor=white&label=pushed
  1058. :target: https://github.com/tqdm/tqdm/pulse
  1059. .. |Gift-Casper| image:: https://img.shields.io/badge/dynamic/json.svg?color=ff69b4&label=gifts%20received&prefix=%C2%A3&query=%24..sum&url=https%3A%2F%2Fcaspersci.uk.to%2Fgifts.json
  1060. :target: https://caspersci.uk.to/donate
  1061. .. |Versions| image:: https://img.shields.io/pypi/v/tqdm.svg
  1062. :target: https://tqdm.github.io/releases
  1063. .. |PyPI-Downloads| image:: https://img.shields.io/pypi/dm/tqdm.svg?label=pypi%20downloads&logo=PyPI&logoColor=white
  1064. :target: https://pypi.org/project/tqdm
  1065. .. |Py-Versions| image:: https://img.shields.io/pypi/pyversions/tqdm.svg?logo=python&logoColor=white
  1066. :target: https://pypi.org/project/tqdm
  1067. .. |Conda-Forge-Status| image:: https://img.shields.io/conda/v/conda-forge/tqdm.svg?label=conda-forge&logo=conda-forge
  1068. :target: https://anaconda.org/conda-forge/tqdm
  1069. .. |Snapcraft| image:: https://img.shields.io/badge/snap-install-82BEA0.svg?logo=snapcraft
  1070. :target: https://snapcraft.io/tqdm
  1071. .. |Docker| image:: https://img.shields.io/badge/docker-pull-blue.svg?logo=docker&logoColor=white
  1072. :target: https://hub.docker.com/r/tqdm/tqdm
  1073. .. |Libraries-Rank| image:: https://img.shields.io/librariesio/sourcerank/pypi/tqdm.svg?logo=koding&logoColor=white
  1074. :target: https://libraries.io/pypi/tqdm
  1075. .. |Libraries-Dependents| image:: https://img.shields.io/librariesio/dependent-repos/pypi/tqdm.svg?logo=koding&logoColor=white
  1076. :target: https://github.com/tqdm/tqdm/network/dependents
  1077. .. |OpenHub-Status| image:: https://www.openhub.net/p/tqdm/widgets/project_thin_badge?format=gif
  1078. :target: https://www.openhub.net/p/tqdm?ref=Thin+badge
  1079. .. |awesome-python| image:: https://awesome.re/mentioned-badge.svg
  1080. :target: https://github.com/vinta/awesome-python
  1081. .. |LICENCE| image:: https://img.shields.io/pypi/l/tqdm.svg
  1082. :target: https://raw.githubusercontent.com/tqdm/tqdm/master/LICENCE
  1083. .. |DOI| image:: https://img.shields.io/badge/DOI-10.5281/zenodo.595120-blue.svg
  1084. :target: https://doi.org/10.5281/zenodo.595120
  1085. .. |notebook-demo| image:: https://img.shields.io/badge/launch-notebook-orange.svg?logo=jupyter
  1086. :target: https://notebooks.ai/demo/gh/tqdm/tqdm
  1087. .. |binder-demo| image:: https://mybinder.org/badge_logo.svg
  1088. :target: https://mybinder.org/v2/gh/tqdm/tqdm/master?filepath=DEMO.ipynb
  1089. .. |Screenshot-Jupyter1| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/tqdm-jupyter-1.gif
  1090. .. |Screenshot-Jupyter2| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/tqdm-jupyter-2.gif
  1091. .. |Screenshot-Jupyter3| image:: https://raw.githubusercontent.com/tqdm/tqdm/master/images/tqdm-jupyter-3.gif
  1092. .. |README-Hits| image:: https://caspersci.uk.to/cgi-bin/hits.cgi?q=tqdm&style=social&r=https://github.com/tqdm/tqdm&l=https://caspersci.uk.to/images/tqdm.png&f=https://raw.githubusercontent.com/tqdm/tqdm/master/images/logo.gif
  1093. :target: https://caspersci.uk.to/cgi-bin/hits.cgi?q=tqdm&a=plot&r=https://github.com/tqdm/tqdm&l=https://caspersci.uk.to/images/tqdm.png&f=https://raw.githubusercontent.com/tqdm/tqdm/master/images/logo.gif&style=social
  1094. .. |sourcerer-0| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/0
  1095. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/0
  1096. .. |sourcerer-1| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/1
  1097. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/1
  1098. .. |sourcerer-2| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/2
  1099. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/2
  1100. .. |sourcerer-3| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/3
  1101. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/3
  1102. .. |sourcerer-4| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/4
  1103. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/4
  1104. .. |sourcerer-5| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/5
  1105. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/5
  1106. .. |sourcerer-6| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/6
  1107. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/6
  1108. .. |sourcerer-7| image:: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/images/7
  1109. :target: https://sourcerer.io/fame/casperdcl/tqdm/tqdm/links/7