METADATA 56 KB

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