METADATA 56 KB

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