annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/tqdm-4.67.1.dist-info/METADATA @ 68:5028fdace37b

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