METADATA 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. Metadata-Version: 2.1
  2. Name: alive-progress
  3. Version: 3.3.0
  4. Summary: A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
  5. Home-page: https://github.com/rsalmei/alive-progress
  6. Author: Rogério Sampaio de Almeida
  7. Author-email: rsalmei@gmail.com
  8. License: MIT
  9. Keywords: progress,bar,progress-bar,progressbar,spinner,eta,monitoring,python,terminal,multithreaded,REPL,alive,animated,visual,feedback,simple,live,efficient,monitor,stats,elapsed,time,throughput
  10. Platform: UNKNOWN
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: Environment :: Console
  14. Classifier: Natural Language :: English
  15. Classifier: License :: OSI Approved :: MIT License
  16. Classifier: Programming Language :: Python :: 3
  17. Classifier: Programming Language :: Python :: 3.9
  18. Classifier: Programming Language :: Python :: 3.10
  19. Classifier: Programming Language :: Python :: 3.11
  20. Classifier: Programming Language :: Python :: 3.12
  21. Classifier: Programming Language :: Python :: 3.13
  22. Classifier: Programming Language :: Python :: 3.14
  23. Requires-Python: >=3.9, <4
  24. Description-Content-Type: text/markdown
  25. License-File: LICENSE
  26. Requires-Dist: about-time==4.2.1
  27. Requires-Dist: graphemeu==0.7.2
  28. [<img align="right" src="https://cdn.buymeacoffee.com/buttons/default-orange.png" width="217px" height="51x">](https://www.buymeacoffee.com/rsalmei)
  29. [<img align="right" alt="Donate with PayPal button" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif">](https://www.paypal.com/donate?business=6SWSHEB5ZNS5N&no_recurring=0&item_name=I%27m+the+author+of+alive-progress%2C+clearly+and+about-time.+Thank+you+for+appreciating+my+work%21&currency_code=USD)
  30. ![alive-progress logo](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-logo.gif)
  31. # alive-progress
  32. [![Maintenance](https://img.shields.io/badge/maintained-yes-brightgreen.svg)](https://gitHub.com/rsalmei/alive-progress/graphs/commit-activity)
  33. [![PyPI version](https://img.shields.io/pypi/v/alive-progress.svg)](https://pypi.python.org/pypi/alive-progress/)
  34. [![PyPI pyversions](https://img.shields.io/pypi/pyversions/alive-progress.svg)](https://pypi.python.org/pypi/alive-progress/)
  35. [![Downloads](https://static.pepy.tech/personalized-badge/alive-progress?period=month&units=international_system&left_color=grey&right_color=orange&left_text=downloads/month)](https://pepy.tech/project/alive-progress)
  36. [![Downloads](https://static.pepy.tech/personalized-badge/alive-progress?period=total&units=international_system&left_color=grey&right_color=orange&left_text=downloads)](https://pepy.tech/project/alive-progress)
  37. ![GitHub Sponsors](https://img.shields.io/github/sponsors/rsalmei)
  38. Have you ever wondered where your lengthy processing was at, and when would it finish? Do you usually hit `RETURN` several times to make sure it didn't crash, or the SSH connection didn't freeze? Have you ever thought it'd be awesome to be able to _pause some processing_ without hassle, return to the Python prompt to manually fix some items, then _seamlessly resume_ it? I did...
  39. I've started this new progress bar thinking about all that, behold the **alive-progress**! 😃
  40. ![alive-progress demo](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-demo.gif)
  41. Introducing the newest concept in progress bars for Python! `alive-progress` is in a class of its own, with an array of cool features that set it apart. Here are a few highlights:
  42. - A mesmerizing **live spinner** that reacts to your actual processing speed, i.e., it dynamically gets faster or slower with your throughput, and clearly shows a lengthy task is in progress, i.e., did not crash even if taking too long (and your SSH connection did not freeze if remote).
  43. - An efficient **multithreaded** bar that updates itself at a fraction of the actual processing speed to keep **CPU usage low** and avoid terminal spamming (1,000,000 iterations per second equates to roughly 60 updates per second), and you can also calibrate it to your liking.
  44. - An accurate **ETA** (Expected Time of Arrival) with an intelligent _Exponential Smoothing Algorithm_ that shows the time to completion, allowing you to plan your time and manage your workload more effectively.
  45. - Automatic **print** and **logging** hooks that provide seamless and efortless integration with any code, even enriching these outputs with the current bar position when they occurred.
  46. - It prints a nice **final receipt** when your processing ends, including the total items, the elapsed time, and the observed throughput.
  47. - It detects **under** and **overflows**, enabling you to track hits, misses, or any desired counts, not necessarily the actually performed iterations.
  48. - You can **suspend** it! That's right, you heard it here first! No other progress bar anywhere in any language has this feature! You can suspend your processing and get back to the Python prompt, adjust or fix some items, and then get back into the same processing as if it had never stopped!
  49. - It is **highly customizable**, with a smorgasbord of ready-to-use spinner and bar styles, as well as several factories to easily create yours! There's even a super powerful `check()` tool that helps you design your own animations! You can see how the generated frames and animation cycles will look like, exploded on your screen, and even see it _alive_ before installing in `alive-progress`! It's the coolest tool in the world! Unleash your creativity!
  50. ---
  51. ## Table of contents
  52. This README is always evolving, so do take a more comprehensive look from time to time... You might find great new details in other sections! 😊
  53. <!-- TOC -->
  54. * [alive-progress](#alive-progress)
  55. * [Table of contents](#table-of-contents)
  56. * [📌 What's new in 3.3 series](#-whats-new-in-33-series)
  57. * [Previous releases](#previous-releases)
  58. * [Using `alive-progress`](#using-alive-progress)
  59. * [Get it](#get-it)
  60. * [Try it](#try-it)
  61. * [Awake it](#awake-it)
  62. * [Master it](#master-it)
  63. * [The `bar` handler](#the-bar-handler)
  64. * [Auto-iterating](#auto-iterating)
  65. * [Modes of operation](#modes-of-operation)
  66. * [Auto and Unknown: Counter](#auto-and-unknown-counter)
  67. * [Manual: Percentage](#manual-percentage)
  68. * [Widgets available](#widgets-available)
  69. * [The different `bar()` handler semantics](#the-different-bar-handler-semantics)
  70. * [Styles](#styles)
  71. * [Configuration](#configuration)
  72. * [Create your own animations](#create-your-own-animations)
  73. * [Intro: How do they work?](#intro-how-do-they-work)
  74. * [A Spinner Compiler, really?](#a-spinner-compiler-really)
  75. * [Spinner Factories](#spinner-factories)
  76. * [Bar Factories](#bar-factories)
  77. * [Advanced](#advanced)
  78. * [The Pause Mechanism](#the-pause-mechanism)
  79. * [Loop-less use](#loop-less-use)
  80. * [FPS Calibration](#fps-calibration)
  81. * [Forcing animations on PyCharm, Jupyter, etc.](#forcing-animations-on-pycharm-jupyter-etc)
  82. * [Interesting facts](#interesting-facts)
  83. * [To do](#to-do)
  84. * [Python End of Life notice](#python-end-of-life-notice)
  85. * [For Python 2.7 and 3.5](#for-python-27-and-35)
  86. * [For Python 3.6](#for-python-36)
  87. * [For Python 3.7 and 3.8](#for-python-37-and-38)
  88. * [License](#license)
  89. <!-- TOC -->
  90. ## 📌 What's new in 3.3 series
  91. The latest `alive-progress` release is finally here, and it brings some exciting improvements! 🎉
  92. Here's a summary of the most notable changes:
  93. - Now the final receipt is available in the alive_bar handle, even after the bar has finished!
  94. - This allows you to access the final receipt data and use it in your code, such as logging or displaying it in a custom way.
  95. - You can change the bar title and text even after it has finished, so you can update the final receipt with new information or context.
  96. - Included the elapsed time in the alive_bar handle, with full precision, so you can access the exact time it took to complete your processing.
  97. Technical changes:
  98. - Changed the `grapheme` dependency which was no longer maintained to `graphemeu`, which is a maintained fork of the original one.
  99. - Added `py.typed` in the distribution to satisfy mypy and other type checking tools.
  100. - Included Python 3.14 support (in CI for now).
  101. ### Previous releases
  102. <details>
  103. <summary>New in 3.2 series</summary>
  104. After about a year of reassuring stability, the new `alive-progress` has finally landed!
  105. The main features and improvements are:
  106. - The print/logging hooks now support multithreading => a highly requested feature: everything is synchronized now so you can print from different threads without issues! No more queues to send messages to the main thread!
  107. - Rounded ETAs for long tasks => now the ETA automatically decreases its precision the higher it gets, making it smoother and easier to read!
  108. - Support for zero and even negative bar increments => now on par with the manual mode, you can call `bar()` with `0` and even `-N` to make it go backwards! Useful when you couldn't make any progress in an iteration or had to roll back something!
  109. And more!
  110. - Custom offset for the enriched print/logging messages => now you can specify a custom `enrich_offset` to use for printed or logged messages, allowing you to start with `on 1:` or continue where you left from previous computations!
  111. - Improved compatibility with PyInstaller => now custom bars, unknown bars, and spinners work when bundled!
  112. - Improved compatibility with Celery => it will just work within Celery tasks!
  113. - drop python 3.7 and 3.8, hello 3.12 and 3.13!
  114. </details>
  115. <details>
  116. <summary>New in 3.1 series</summary>
  117. A very cool update here! In addition to polishing things up and improving terminal support, now `alive-progress` supports resuming computations!
  118. When processing huge datasets or things that take a long time, you might either use batches or cache partial results. Then, in case it stops and is restarted, you end up skipping all those already done items very quickly, which makes the `alive_bar` think you're processing thousands of items per second, which in turn completely ruins the ETA... But not anymore! Just tell `bar()` that you've skipped items... 👏
  119. You can use it in two ways:
  120. <strong>1.</strong> If you do know where you've stopped:
  121. ```python
  122. with alive_bar(120000) as bar:
  123. bar(60000, skipped=True)
  124. for i in range(60000, 120000):
  125. # process item
  126. bar()
  127. ```
  128. Yep, just call `bar(N, skipped=True)` once, with the number of items.
  129. <strong>2.</strong> If you do not know or the items are scattered:
  130. ```python
  131. with alive_bar(120000) as bar:
  132. for i in range(120000):
  133. if done(i):
  134. bar(skipped=True)
  135. continue
  136. # process item
  137. bar()
  138. ```
  139. Yep, it's as simple as that! Just call `bar(skipped=True)` when an item is already done, or `bar()` as usual otherwise. You could also share a single `bar(skipped=?)` call at the end, with a bool saying whether you did skip that item or not. Cool, huh?
  140. Also in this version:
  141. - new `max_cols` config setting, the number of columns to use if not possible to fetch it, like in jupyter and other platforms which doesn't support size
  142. - fix fetching the size of the terminal when using stderr
  143. - officially supports Python 3.11
  144. - included [ruff](https://github.com/charliermarsh/ruff) linter before building
  145. </details>
  146. <details>
  147. <summary>New in 3.0 series</summary>
  148. Yep, I could finally get this version out! These are the new goodies:
  149. - Units support! You can now label the data you're processing, like `B`, `bytes`, or even `°C`!
  150. - Automatic scaling! With support for SI (base 1000), IEC (base 1024), and even an alternate SI with base 1024, you'll be well served!
  151. - Configurable precision! When your numbers are scaled, you get to choose how many decimals they display!
  152. - Automatic stats scaling for slow throughputs! If your processing takes minutes or more, now you'll see rates per minute, per hour, and even per day! (It works within the auto-scaling system!)
  153. - Support for using `sys.stderr` and other files instead of `sys.stdout`!
  154. - Smoothed out the rate estimation with the same Exponential Smoothing Algorithm that powers the ETA, so the bar returns a more realistic ETA!
  155. - Query the currently running widgets' data, like the monitor, rate, and ETA!
  156. - New help system on configuration errors, which explains why a value was not accepted, and what were the expected ones!
  157. Highly anticipated fixes:
  158. - Support for reusing logging handlers! No more `TypeError: unhashable type: 'types.SimpleNamespace'`.
  159. - Support for logging when using `RotatingFileHandler`s! Yep, seek support is here.
  160. - Fix unknown mode always ending with a warning (!)
  161. And last but not least, a more polished layout for you to enjoy your progress!
  162. ![alive-progress 3.0](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive3.0.png)
  163. </details>
  164. <details>
  165. <summary>New in 2.4 series</summary>
  166. Now, `alive_bar` supports *Dual Line* text mode!
  167. If you ever wanted to include longer situational messages within the bar, you probably felt squeezed into one line. You had to shrink the beautifully animated bar or, even worse, remove widgets (!) to be able to see what you needed...
  168. <br>Not anymore!! You can now make the bar *Dual Line*, and put text below it!
  169. Yes, there's a message below the whole bar, and any other print/logging messages scroll above it!
  170. ```python
  171. letters = [chr(ord('A') + x) for x in range(26)]
  172. with alive_bar(26, dual_line=True, title='Alphabet') as bar:
  173. for c in letters:
  174. bar.text = f'-> Teaching the letter: {c}, please wait...'
  175. if c in 'HKWZ':
  176. print(f'fail "{c}", retry later')
  177. time.sleep(0.3)
  178. bar()
  179. ```
  180. Output:
  181. ```
  182. on 7: fail "H", retry later
  183. on 10: fail "K", retry later
  184. Alphabet |███████████████████████████▊ | ▃▅▇ 18/26 [69%] in 6s (3.2/s, eta: 3s)
  185. -> Teaching the letter: S, please wait...
  186. ```
  187. There's also a new `finalize` function parameter in `alive_it` which enables you to set the title and/or text of the final receipt, and improved logging support which detects customized loggers.
  188. </details>
  189. <details>
  190. <summary>New in 2.3 series</summary>
  191. This is all about customization; the core widgets can now be changed:
  192. - send a string to the `monitor`, `elapsed`, and `stats` widgets to make them look anyway you want!
  193. > It's incredible that these strings support all Python format features, so you can e.g., `{percent:.1%}` 😉.
  194. They can be further customized when on the **final receipt**!
  195. - new `monitor_end`, `elapsed_end`, and `stats_end`, with dynamic formats inherited from the standard ones!
  196. > If you've hidden some widgets before, just so they wouldn't appear on the receipt, now you can see them in all their running glory, and hide just the receipt ones! Or the other way around 😜
  197. Another addition, now `alive-progress` beautifully renders its cool final receipt whenever it is stopped, even if you CTRL+C it prematurely! I don't know why I haven't thought about that before...
  198. ```
  199. Download |██████████████████⚠︎ | (!) 45/100 [45%] in 4.8s (9.43/s)
  200. ```
  201. And finally, you can choose to disable CTRL+C at all! The default is the safer `ctrl_c=True`, which does make CTRL-C work as usual.
  202. <br>Disable it `ctrl_c=False`, to make your interactive `alive_bar` much smoother to use (there are no stack traces if you stop it), and/or if it is at the top-level of your program!
  203. > Beware: If it is e.g. inside a for-loop, it will just continue to the next iteration, which may or may not be what you want...
  204. ```python
  205. for i in range(10):
  206. with alive_bar(100, ctrl_c=False, title=f'Download {i}') as bar:
  207. for i in range(100):
  208. time.sleep(0.02)
  209. bar()
  210. ```
  211. Output:
  212. ```
  213. Download 0 |████████▊⚠︎ | (!) 22/100 [22%] in 0.6s (36.40/s)
  214. Download 1 |████████████████▊⚠︎ | (!) 42/100 [42%] in 1.0s (41.43/s)
  215. Download 2 |██████▍⚠︎ | (!) 16/100 [16%] in 0.4s (39.29/s)
  216. Download 3 |█████▋⚠︎ | (!) 14/100 [14%] in 0.4s (33.68/s)
  217. Download 4 |█████████████▎⚠︎ | (!) 33/100 [33%] in 0.8s (39.48/s)
  218. Download 5 |███████▎⚠︎ | (!) 18/100 [18%] in 0.5s (37.69/s)
  219. Download 6 |█████▎⚠︎ | (!) 13/100 [13%] in 0.3s (37.28/s)
  220. Download 7 |████████████⚠︎ | (!) 30/100 [30%] in 0.8s (38.43/s)
  221. Download 8 |██████⚠︎ | (!) 15/100 [15%] in 0.4s (36.26/s)
  222. ...
  223. ```
  224. </details>
  225. <details>
  226. <summary>New in 2.2 series</summary>
  227. Some major new features, often requested, have finally landed!
  228. - bar title can be dynamically set, changed, or even removed after being displayed
  229. - new custom fps system, which enables very slow refresh rates (to let it run on those k8s for long periods)
  230. - the final receipt can be totally hidden (great for special effects, like using the cool spinners standalone)
  231. - new support for `click.echo()` printing
  232. - terminal columns detection is safer for exotic environments
  233. - requires Python 3.7+
  234. </details>
  235. <details>
  236. <summary>New in 2.1 series</summary>
  237. YES! Now `alive-progress` has support for Jupyter Notebooks and also includes a _Disabled_ state! Both were highly sought after, and have finally landed!
  238. <br>And better, I've implemented an auto-detection mechanism for jupyter notebooks, so it just works, out of the box, without any changes in your code!!
  239. See for yourself:
  240. ![alive-progress demo](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-jupyter.gif)
  241. > It seems to work very well, but at this moment, it should be considered **experimental**.
  242. > <br>There were instances in which some visual glitches have appeared, like two `alive_bar` refreshes being concatenated together instead of over one another... And it's something I think I can't possibly work around: it seems Jupyter sometimes refresh the canvas at odd times, which makes it lose some data. Please let me know on the issues if something funnier arises.
  243. </details>
  244. <details>
  245. <summary>New in 2.0 series</summary>
  246. This is a major breakthrough in `alive-progress`!
  247. <br>I took 1 year developing it, and I'm very proud of what I've accomplished \o/
  248. - now, there's complete support for Emojis 🤩 and exotic Unicode chars in general, which required MAJOR refactoring deep within the project, giving rise to what I called "**Cell Architecture**" => now, all internal components use and generate streams of cells instead of characters, and correctly interpret grapheme clusters — those so-called wide chars, which are encoded with a variable number of chars, but always take two cells on screen!! This has enabled us to render complex multi-chars symbols as if they were one, thus making them work on any spinners, bars, texts, borders and backgrounds, even when fractured!!! Pretty advanced stuff 🤓
  249. - new super cool spinner compiler and runner, which generates complete animations ahead of time, and plays these ready-to-go animations seamlessly, with no overhead at all! 🚀
  250. - the spinner compiler also includes advanced extra commands to generate and modify animations, like reshape, replace, transpose, or randomize the animation cycles!
  251. - new powerful and polished `.check()` tools that compile and beautifully render all frames from all animation cycles of spinners and bars! they can even include complete frame data, internal codepoints, and even their animations! 👏
  252. - bars engine revamp, with invisible fills, advanced support for multi-char tips (which gradually enter and leave the bar), borders, tips and errors of any length, and underflow errors that can leap into the border if they can't fit!
  253. - spinners engine revamp, with standardized factory signatures, improved performance, new types, and new features: smoother bouncing spinners (with an additional frame at the edges), optimized scrolling of text messages (which go slower and pause for a moment at the edges), new alongside and sequential spinners, nicer effect in alongside spinners (which use weighted spreading over the available space), smoother animation in scrolling spinners (when the input is longer than the available space)
  254. - new builtin spinners, bars, and themes, which make use of the new animation features
  255. - new showtime that displays themes and is dynamic => it does not scroll the screen when it can't fit vertically or horizontally, and can even filter for patterns!
  256. - improved support for logging into files, which gets enriched as the print hook is!
  257. - several new configuration options for customizing appearance, including support for disabling any `alive-progress` widgets!
  258. - includes a new iterator adapter, `alive_it`, that accepts an iterable and calls `bar()` for you!
  259. - requires Python 3.6+ (and officially supports Python 3.9 and 3.10)
  260. > Since this is a major version change, direct backward compatibility is not guaranteed. If something does not work at first, just check the new imports and functions' signatures, and you should be good to go. All previous features should still work here! 👍
  261. </details>
  262. ---
  263. ## Using `alive-progress`
  264. ### Get it
  265. Just install with pip:
  266. ```sh
  267. ❯ pip install alive-progress
  268. ```
  269. ### Try it
  270. If you're wondering what styles are builtin, it's `showtime`! ;)
  271. ```python
  272. from alive_progress.styles import showtime
  273. showtime()
  274. ```
  275. > Note: Please disregard the path in the animated gif below, the correct one is above. These long gifs are very time-consuming to generate, so I can't make another on every single change. Thanks for your understanding.
  276. ![alive-progress spinners](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/showtime-spinners.gif)
  277. I've made these styles just to try all the animation factories I've created, but I think some of them ended up very, very cool! Use them at will, and mix them to your heart's content!
  278. Do you want to see actual `alive-progress` bars gloriously running in your system before trying them yourself?
  279. ```sh
  280. ❯ python -m alive_progress.tools.demo
  281. ```
  282. ![alive-progress demo-tool](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-demo-tool.png)
  283. ### Awake it
  284. Cool, huh?? Now enter an `ipython` REPL and try this:
  285. ```python
  286. from alive_progress import alive_bar
  287. import time
  288. for x in 1000, 1500, 700, 0:
  289. with alive_bar(x) as bar:
  290. for i in range(1000):
  291. time.sleep(.005)
  292. bar()
  293. ```
  294. You'll see something like this, with cool animations throughout the process 😜:
  295. ```
  296. |████████████████████████████████████████| 1000/1000 [100%] in 5.8s (171.62/s)
  297. |██████████████████████████▋⚠︎ | (!) 1000/1500 [67%] in 5.8s (172.62/s)
  298. |████████████████████████████████████████✗︎ (!) 1000/700 [143%] in 5.8s (172.06/s)
  299. |████████████████████████████████████████| 1000 in 5.8s (172.45/s)
  300. ```
  301. Nice, huh? Loved it? I knew you would, thank you 😊.
  302. To actually use it, just wrap your normal loop in an `alive_bar` context manager like this:
  303. ```python
  304. with alive_bar(total) as bar: # declare your expected total
  305. for item in items: # <<-- your original loop
  306. print(item) # process each item
  307. bar() # call `bar()` at the end
  308. ```
  309. And it's alive! 👏
  310. So, in a nutshell: retrieve the items as always, enter the `alive_bar` context manager with the number of items, and then iterate/process those items, calling `bar()` at the end! It's that simple! :)
  311. ### Master it
  312. - The `items` argument can be any iterable, like for example, a queryset.
  313. - The first argument of the `alive_bar` is the expected total, like `qs.count()` for querysets, `len(items)` for bounded iterables, or even some static number.
  314. - Calling `bar()` is what makes the bar go forward — you usually call it once in every iteration, just after finishing an item.
  315. - If you call `bar()` too much or too few, the bar will graphically render that deviation from the expected `total`, making it very easy to notice under and overflows.
  316. - The usual Python `print()` is automatically hooked, so you can effortlessly display messages tightly integrated with the current progress bar on display! It won't break the fast bar refreshes in any way and will even enrich your message! The bar nicely cleans up the line, prints the current bar position alongside your message, and continues refreshing right below it!
  317. - The standard Python `logging` framework is also automatically hooked and enriched, exactly like the `print()` above!
  318. - If you're using the `click` CLI lib, you can even use `click.echo()` to print styled text.
  319. ![alive-progress printing messages](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/print-hook.gif)
  320. > You can get creative! Since the bar goes forward only when you call `bar()`, it is **independent of the loop** it is in! So you can use it to monitor unrelated things like pending transactions, broken items, etc., or even call it more than once in the same iteration! That way, you'll get to know how many of these "special" events there were, including their percentage relative to the total!
  321. Awesome right? And everything work the same both in real terminals and in Jupyter notebooks!
  322. ## The `bar` handler
  323. After a bar has finished (or even while running), you have a plethora of methods available to grab information about the bar, and to control it. Here are they:
  324. - `bar.text('message')` or `bar.text = 'message'`: set a situational message at the end of the bar, where you can display information about the current item or the phase the processing is in.
  325. - `bar.title('Title')` or `bar.title = 'Title'`: set a title at the beginning of the bar; can be set right when starting it or while it's running or even after finished to affect the receipt on demand — mix it with `title_length` config to keep the bar from changing its length while running.
  326. - `bar.current`: retrieve the current bar count or percentage — more details below on Modes of Operation.
  327. - `bar.monitor` returns the current monitor widget text, which is the current bar position formatted according to the current configuration.
  328. - `bar.eta`: returns the current ETA widget text, formatted according to the current configuration.
  329. - `bar.rate`: returns the current throughput widget text, formatted according to the current configuration.
  330. - `bar.elapsed`: returns the current elapsed time in seconds, with full precision.
  331. - `bar.receipt`: returns an on-demand receipt, which can be used however you want in your code, such as logging or displaying it in a custom way.
  332. - `bar.pause()`: pauses the bar without losing its state — more details below on the Pause Mechanism.
  333. ## Auto-iterating
  334. You now have a quicker way to monitor anything! Here, the items are automatically tracked for you!
  335. <br>Behold the `alive_it` => the `alive_bar` iterator adapter!
  336. Simply wrap your items with it, and loop over them as usual!
  337. <br>The bar will just work; it's that simple!
  338. ```python
  339. from alive_progress import alive_it
  340. for item in alive_it(items): # <<-- wrapped items
  341. print(item) # process each item
  342. ```
  343. HOW COOL IS THAT?! 😜
  344. All `alive_bar` parameters apply but `total`, which is smarter (if not supplied, it will be auto-inferred from your data using `len` or `length_hint`), and `manual` that does not make sense here.
  345. Note there isn't any `bar` handle at all in there. But what if you do want it, e.g. to set text messages or retrieve the current progress?
  346. <br>You can interact with the internal `alive_bar` by just assigning `alive_it` to a variable like this:
  347. ```python
  348. bar = alive_it(items) # <<-- bar with wrapped items
  349. for item in bar: # <<-- iterate on bar
  350. print(item) # process each item
  351. bar.text(f'ok: {item}') # WOW, it works!
  352. ```
  353. Note that this is a slightly special `bar`, which does not support `bar()`, since the iterator adapter tracks items automatically for you. Also, it supports `finalize`, which enables you to set the title and/or text of the final receipt:
  354. ```python
  355. alive_it(items, finalize=lambda bar: bar.text('Success!'))
  356. ...
  357. ```
  358. > In a nutshell:
  359. > - full use is always `with alive_bar() as bar`, where you iterate and call `bar()` whenever you want;
  360. > - quick adapter use is `for item in alive_it(items)`, where items are automatically tracked;
  361. > - full adapter use is `bar = alive_it(items)`, where in addition to items being automatically tracked, you get a special iterable `bar` able to customize the inner `alive_progress` however you want.
  362. ## Modes of operation
  363. ### Auto and Unknown: Counter
  364. The default modes are **auto** and **unknown**, which use internally a **counter** to track the progress. They count the number of items processed, and use it to update the progress bar accordingly.
  365. The `total` argument is optional. If you do provide it, the bar enters in **auto mode**. In this mode, the progress of the operation is automatically tracked, and all the widgets `alive-progress` has to offer are available: precise bar, spinner, percentage, counter, throughput, and ETA.
  366. If you don't provide `total`, the bar enters in **unknown mode**. In this mode, the progress is indeterminable, and therefore the ETA, so the whole progress bar is continuously animated. The widgets available are: animated bar, spinner, counter, and throughput.
  367. > The cool spinner runs completely independently of the animated bar, both running their own animations concurrently, rendering a unique show in your terminal! 😜
  368. Last but not least, the **auto** mode has a unique ability: mark items as skipped, making the throughput and ETA much more accurate! More on that later.
  369. ### Manual: Percentage
  370. The **manual mode**, manually activated by the `manual=True` argument, uses internally a **percentage** to track the progress. It enables you to get complete control of the bar position. It's usually used to monitor processes that only feed you a percentage of completion, or to generate some random special effects.
  371. You can use it directly with `alive_bar` or via `config_handler`, and it allows you to send percentages to the `bar()` handler! For example, to set it to 15% completion, just call `bar(0.15)` — which is 15 / 100.
  372. You can also provide `total` here. If you do, `alive-progress` will automatically infer an internal counter, and will thus be able to offer you all the same widgets available in auto mode!
  373. If you don't provide `total`, you'll at least get rough versions of the throughput and ETA widgets, computed as "%/s" (percentage per second) and until 100%, respectively. Neither of them are very accurate, but they are better than nothing.
  374. ### Widgets available
  375. When `total` is provided all is cool:
  376. | mode | counter | percentage | throughput | ETA | over/underflow |
  377. |:------:|:-------------:|:------------:|:----------:|:---:|:--------------:|
  378. | auto | ✅ (user tick) | ✅ (inferred) | ✅ | ✅ | ✅ |
  379. | manual | ✅ (inferred) | ✅ (user set) | ✅ | ✅ | ✅ |
  380. When it isn't, some compromises have to be made:
  381. | mode | counter | percentage | throughput | ETA | over/underflow |
  382. |:-------:|:-------------:|:------------:|:------------:|:----------:|:--------------:|
  383. | unknown | ✅ (user tick) | ❌ | ✅ | ❌ | ❌ |
  384. | manual | ❌ | ✅ (user set) | ⚠️ (simpler) | ⚠️ (rough) | ✅ |
  385. But it's actually simple to understand: you do not need to think about which mode you should use!
  386. - Just always send the `total` if you have it, and use `manual` if you need it!
  387. That's it! It will just work the best it can! 👏 \o/
  388. ### The different `bar()` handler semantics
  389. The `bar()` handlers support either relative or absolute semantics, depending on the mode:
  390. - _auto_ and _unknown_ modes use **optional relative** positioning, so you can just call `bar()` to increment the counter by one, or send any other increment like `bar(200)` to increment by 200 at once;
  391. > they even support `bar(0)` and `bar(-5)` to hold or decrement if needed!
  392. - _manual_ mode uses **mandatory absolute** positioning, so you can call `bar(0.35)` to make the bar instantly jump to 35% progress.
  393. > Both modes enable you to get creative! Since you can just make the bar go instantly to whatever position you want, you can:
  394. > - make it go backwards — e.g. to graphically display the timeout of something;
  395. > - create special effects — e.g. to mimic a real-time analog gauge of some sort.
  396. You can call `bar()` as many times as you want! The terminal refresh rate will always be asynchronously computed according to the current throughput and progress, so you won't risk spamming the terminal with more updates than needed.
  397. In any case, to retrieve the current counter/percentage, just call: `bar.current`:
  398. - in _auto_ and _unknown_ modes, this provides an **integer** — the actual internal counter;
  399. - in _manual_ mode, this provides a **float** in the interval [0, 1] — the last percentage set.
  400. Finally, the `bar()` handler in **auto** mode leverages a unique ability: skip items! Just call `bar(skipped=True)` or `bar(10, skipped=True)` to exclude those items from the throughput calculations, and thus preventing them from inaccurately affecting the ETA.
  401. ---
  402. Maintaining an open source project is hard and time-consuming, and I've put much ❤️ and effort into this.
  403. If you've appreciated my work, you can back me up with a donation! Thank you 😊
  404. [<img align="right" src="https://cdn.buymeacoffee.com/buttons/default-orange.png" width="217px" height="51x">](https://www.buymeacoffee.com/rsalmei)
  405. [<img align="right" alt="Donate with PayPal button" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif">](https://www.paypal.com/donate?business=6SWSHEB5ZNS5N&no_recurring=0&item_name=I%27m+the+author+of+alive-progress%2C+clearly+and+about-time.+Thank+you+for+appreciating+my+work%21&currency_code=USD)
  406. ---
  407. ## Styles
  408. The `showtime` exhibit has an optional argument to choose which show to present, `Show.SPINNERS` (default), `Show.BARS` or `Show.THEMES`, do take a look at them! ;)
  409. ```python
  410. from alive_progress.styles import showtime, Show
  411. showtime(Show.BARS)
  412. showtime(Show.THEMES)
  413. ```
  414. > Note: Please disregard the path in the animated gif below, the correct one is above. These long gifs are very time-consuming to generate, so I can't make another on every single change. Thanks for your understanding.
  415. ![alive-progress bars](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/showtime-bars.gif)
  416. And the themes one (📌 new in 2.0):
  417. ![alive-progress themes](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/showtime-themes.gif)
  418. The `showtime` exhibit also accepts some customization options:
  419. - **fps**: the frames per second rate refresh rate, default is 15;
  420. - **length**: the length of the bars, default is 40;
  421. - **pattern**: a filter to choose which ones to display.
  422. For example to get a marine show, you can `showtime(pattern='boat|fish|crab')`:
  423. ![alive-progress filtered spinners](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/showtime-marine-spinners.gif)
  424. > You can also access these shows with the shorthands `show_bars()`, `show_spinners()`, and `show_themes()`!
  425. > There's also a small utility called `print_chars()`, to help find that cool character to put in your customized spinners and bars, or to determine if your terminal does support Unicode characters.
  426. ## Configuration
  427. There are several options to customize both appearance and behavior!
  428. <br>All of them can be set both directly in the `alive_bar` or globally in the `config_handler`!
  429. These are the options - default values in brackets:
  430. - `title`: an optional, always visible bar title
  431. - `length`: [`40`] the number of cols to render the animated progress bar
  432. - `max_cols`: [`80`] the maximum cols to use if not possible to fetch it, like in jupyter
  433. - `spinner`: the spinner style to be rendered next to the bar
  434. <br> ↳ accepts a predefined spinner name, a custom spinner factory, or None
  435. - `bar`: the bar style to be rendered in known modes
  436. <br> ↳ accepts a predefined bar name, a custom bar factory, or None
  437. - `unknown`: the bar style to be rendered in the unknown mode
  438. <br> ↳ accepts a predefined spinner name, or a custom spinner factory (cannot be None)
  439. - `theme`: [`'smooth'`] a set of matching spinner, bar, and unknown
  440. <br> ↳ accepts a predefined theme name
  441. - `force_tty`: [`None`] forces animations to be on, off, or according to the tty (more details [here](#forcing-animations-on-pycharm-jupyter-etc))
  442. <br> ↳ None -> auto select, according to the terminal/Jupyter
  443. <br> ↳ True -> unconditionally enables animations, but still auto-detects Jupyter Notebooks
  444. <br> ↳ False -> unconditionally disables animations, keeping only the final receipt
  445. - `file`: [`sys.stdout`] the file object to use: `sys.stdout`, `sys.stderr`, or a similar `TextIOWrapper`
  446. - `disable`: [`False`] if True, completely disables all output, do not install hooks
  447. - `manual`: [`False`] set to manually control the bar position
  448. - `enrich_print`: [`True`] enriches print() and logging messages with the bar position
  449. - `enrich_offset`: [`0`] the offset to apply to enrich_print
  450. - `receipt`: [`True`] prints the nice final receipt, disables if False
  451. - `receipt_text`: [`False`] set to repeat the last text message in the final receipt
  452. - `monitor` (bool|str): [`True`] configures the monitor widget `152/200 [76%]`
  453. <br> ↳ send a string with `{count}`, `{total}` and `{percent}` to customize it
  454. - `elapsed` (bool|str): [`True`] configures the elapsed time widget `in 12s`
  455. <br> ↳ send a string with `{elapsed}` to customize it
  456. - `stats` (bool|str): [`True`] configures the stats widget `(123.4/s, eta: 12s)`
  457. <br> ↳ send a string with `{rate}` and `{eta}` to customize it
  458. - `monitor_end` (bool|str): [`True`] configures the monitor widget within final receipt
  459. <br> ↳ same as monitor, the default format is dynamic, it inherits `monitor`'s one
  460. - `elapsed_end` (bool|str): [`True`] configures the elapsed time widget within final receipt
  461. <br> ↳ same as elapsed, the default format is dynamic, it inherits `elapsed`'s one
  462. - `stats_end` (bool|str): [`True`] configures the stats widget within final receipt
  463. <br> ↳ send a string with `{rate}` to customize it (no relation to stats)
  464. - `title_length`: [`0`] fixes the length of titles, or 0 for unlimited
  465. <br> ↳ title will be truncated if longer, and a cool ellipsis "…" will appear at the end
  466. - `spinner_length`: [`0`] forces the spinner length, or `0` for its natural one
  467. - `refresh_secs`: [`0`] forces the refresh period to this, `0` is the reactive visual feedback
  468. - `ctrl_c`: [`True`] if False, disables CTRL+C (captures it)
  469. - `dual_line`: [`False`] if True, places the text below the bar
  470. - `unit`: any text that labels your entities
  471. - `scale`: the scaling to apply to units: `None`, `SI`, `IEC`, or `SI2`
  472. <br> ↳ supports aliases: `False` or `''` -> `None`, `True` -> `SI`, `10` or `'10'` -> `SI`, `2` or `'2'` -> `IEC`
  473. - `precision`: [`1`] how many decimals do display when scaling
  474. And there's also one that can only be set locally in the `alive_bar` context:
  475. - `calibrate`: maximum theoretical throughput to calibrate the animation speed (more details [here](#fps-calibration))
  476. To set them locally, just send them as keyword arguments to `alive_bar`:
  477. ```python
  478. with alive_bar(total, title='Processing', length=20, bar='halloween') as bar:
  479. ...
  480. ```
  481. To use them globally, send them to `config_handler`, and any `alive_bar` created after that will include those options! And you can mix and match them, local options always have precedence over global ones:
  482. ```python
  483. from alive_progress import config_handler
  484. config_handler.set_global(length=20, spinner='wait')
  485. with alive_bar(total, bar='blocks', spinner='twirls') as bar:
  486. # the length is 20, the bar is 'blocks' and the spinner is 'twirls'.
  487. ...
  488. ```
  489. ## Create your own animations
  490. Yes, you can assemble your own spinners! And it's easy!
  491. <br>I've created a plethora of special effects, so you can just mix and match them any way you want! There are frames, scrolling, bouncing, sequential, alongside, and delayed spinners! Get creative! 😍
  492. ### Intro: How do they work?
  493. The spinners' animations are engineered by very advanced generator expressions, deep within several layers of meta factories, factories and generators 🤯!
  494. - the meta factory (public interface) receives the styling parameters from you, the user, and processes/stores them inside a closure to create the actual factory => this is the object you'll send to both `alive_bar` and `config_handler`;
  495. - internally it still receives other operating parameters (like for instance the rendition length), to assemble the actual generator expression of the animation cycles of some effect, within yet another closure;
  496. - this, for each cycle, assembles another generator expression for the animation frames of the same effect;
  497. - these generators together finally produce the streams of cycles and frames of the cool animations we see on the screen! Wow! 😜👏
  498. These generators are capable of multiple different animation cycles according to the spinner behavior, e.g. a bouncing spinner can run one cycle to smoothly bring a subject into the scene, then repeatedly reposition it until the other side, then make it smoothly disappear off the scene => and this is all only one cycle! Then it can be followed by another cycle to make it all again but backwards!
  499. And bouncing spinners also accept _different_ and _alternating_ patterns in both the right and left directions, which makes them generate the cartesian product of all the combinations, possibly producing dozens of different cycles until they start repeating them!! 🤯
  500. And there's more, I think one of the most impressive achievements I got in this animation system (besides the spinner compiler itself)... They only yield more animation frames until the current cycle is not exhausted, then **they halt themselves**! Yep, the next cycle does not start just yet! This behavior creates natural breaks in exactly the correct spots, where the animations are not disrupted, so I can smoothly link with whatever other animation I want!!
  501. <br>This has all kinds of cool implications: the cycles can have different frame counts, different screen lengths, they do not need to be synchronized, they can create long different sequences by themselves, they can cooperate to play cycles in sequence or alongside, and I can amaze you displaying several totally distinct animations at the same time without any interferences whatsoever!
  502. > It's almost like they were... _alive_!! 😄
  503. > <br>==> Yes, that's where this project's name came from! 😉
  504. ### A Spinner Compiler, really?
  505. Now, these generators of cycles and frames are fully consumed ahead of time by the **Spinner Compiler**! This is a very cool new processor that I made inside the _Cell Architecture_ effort, to make all these animations work even in the presence of wide chars or complex grapheme clusters! It was very hard to make these clusters gradually enter and exit frames, smoothly, while keeping them from breaking the Unicode encoding and especially maintain their original lengths in all frames! Yes, several chars in sequence can represent another completely different symbol, so they cannot ever be split! They have to enter and exit the frame always together, all at once, or the grapheme won't show up at all (an Emoji for instance)!! Enter the **Spinner Compiler**......
  506. This has made possible some incredible things!! Since this Compiler generates the whole spinner frame data beforehand:
  507. - the grapheme fixes can be applied only once;
  508. - the animations do not need to be calculated again!
  509. > So, I can just collect all that _ready to play_ animations and be done with it, **no runtime overhead** at all!! 👏
  510. Also, with the complete frame data compiled and persisted, I could create several commands to **refactor** that data, like changing shapes, replacing chars, adding visual pauses (frame repetitions), generating bouncing effects on-demand over any content, and even transposing cycles with frames!!
  511. But how can you see these effects? Does the effect you created look good? Or is it not working as you thought? YES, now you can see all generated cycles and frames analytically, in a very beautiful rendition!!
  512. <br>I love what I've achieved here 😊, it's probably THE most beautiful tool I've ever created... Behold the `check` tool!!
  513. ![alive-progress check tool](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-spinner-check.png)
  514. It's awesome if I say so myself, isn't it? And a very complex piece of software I'm proud of, [take a look at its code](alive_progress/animations/spinner_compiler.py) if you'd like.
  515. And the `check` tool is much more powerful! For instance, you can see the codepoints of the frames!!! And maybe have a glimpse of why this version was so, so very hard and complex to make...
  516. ![alive-progress check tool](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-spinner-check-codepoints.png)
  517. In red, you see the grapheme clusters, that occupy one or two "logical positions", regardless of their actual sizes... These are the "Cells" of the new _Cell Architecture_...
  518. <br>Look how awesome an Emoji Flag is represented:
  519. ![alive-progress check tool](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-spinner-check-codepoints-flag.png)
  520. The flag seems to move so smoothly because it uses "half-characters"! Since it is a wide char, `alive-progress` knows it will be rendered with "two visible chars", and the animations consider this, but compose with spaces, which occupy only one. When one uses mixed backgrounds, the situation is much more complex...
  521. ### Spinner Factories
  522. The types of factories I've created are:
  523. - `frames`: draws any sequence of characters at will, that will be played frame by frame in sequence;
  524. - `scrolling`: generates a smooth flow from one side to the other, hiding behind or wrapping upon invisible borders — allows using subjects one at a time, generating several cycles of distinct characters;
  525. - `bouncing`: similar to `scrolling`, but makes the animations bounce back to the start, hiding behind or immediately bouncing upon invisible borders;
  526. - `sequential` get a handful of factories and play them one after the other sequentially! allows to intermix them or not;
  527. - `alongside` get a handful of factories and play them alongside simultaneously, why choose when you can have them all?! allows to choose the pivot of the animation;
  528. - `delayed`: get any other factory and copy it multiple times, increasingly skipping some frames on each one! very cool effects are made here!
  529. For more details please look at their docstrings, which are very complete.
  530. ### Bar Factories
  531. Customizing bars is nowhere near that involved. Let's say they are "immediate", passive objects. They do not support animations, i.e. they will always generate the same rendition given the same parameters. Remember spinners are infinite generators, capable of generating long and complex sequences.
  532. Well, bars also have a meta factory, use closures to store the styling parameters, and receive additional operating parameters, but then the actual factory can't generate any content by itself. It still needs an extra parameter, a floating-point number between 0 and 1, which is the percentage to render itself.
  533. > `alive_bar` calculates this percentage automatically based on the counter and total, but you can send it yourself when in the `manual` mode!
  534. Bars also do not have a Bar Compiler, but they **do provide the check tool**!! 🎉
  535. ![alive-progress check tool](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-bar-check.png)
  536. You can even mix and match wide chars and normal chars just like in spinners! (and everything keeps perfectly aligned 😅)
  537. ![alive-progress check tool](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-bar-check-mix.png)
  538. > Use the check tools to your heart's content!! They have even more goodies awaiting you, even real-time animations!
  539. > Create the wildest and coolest animations you can and send them to me!
  540. > <br>I'm thinking about creating some kind of `contrib` package, with user-contributed spinners and bars!
  541. Wow, if you've read everything till here, you should now have a sound knowledge about using `alive-progress`! 👏
  542. <br>But brace yourself because there is even more, exciting stuff lies ahead!
  543. ---
  544. Maintaining an open source project is hard and time-consuming, and I've put much ❤️ and effort into this.
  545. If you've appreciated my work, you can back me up with a donation! Thank you 😊
  546. [<img align="right" src="https://cdn.buymeacoffee.com/buttons/default-orange.png" width="217px" height="51x">](https://www.buymeacoffee.com/rsalmei)
  547. [<img align="right" alt="Donate with PayPal button" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif">](https://www.paypal.com/donate?business=6SWSHEB5ZNS5N&no_recurring=0&item_name=I%27m+the+author+of+alive-progress%2C+clearly+and+about-time.+Thank+you+for+appreciating+my+work%21&currency_code=USD)
  548. ---
  549. ## Advanced
  550. ### The Pause Mechanism
  551. Oh, you want to pause it altogether, I hear? This is an amazing novel concept, not found anywhere AFAIK.
  552. <br>With this you get to act on some items **manually**, at will, right in the middle of an ongoing processing!!
  553. <br>YES, you can return to the prompt and fix, change, submit things, and the bar will just "remember" where it was...
  554. Suppose you need to reconcile payment transactions (been there, done that). You need to iterate over thousands of them, detect somehow the faulty ones, and fix them. This fix is not simple nor deterministic, you need to study each one to understand what to do. They could be missing a recipient, or have the wrong amount, or not be synced with the server, etc., it's hard to even imagine all possibilities.
  555. Typically, you would have to let the detection process run until completion, appending to a list each inconsistency it finds and waiting, potentially a long time, until you can finally start fixing them... You could of course mitigate that by processing in chunks, or printing them and acting via another shell, etc., but those have their own shortcomings... 😓
  556. <br>Now, there's a better way! Simply pause the actual detection process for a while! Then you just have to wait till the next fault is found, and act in near real-time!
  557. To use the pause mechanism you just have to write a function, so the code can `yield` the items you want to interact with. You most probably already use one in your code, but in the `ipython` shell or another REPL you probably don't. So just wrap your debug code in a function, then enter within a `bar.pause()` context!!
  558. ```python
  559. def reconcile_transactions():
  560. qs = Transaction.objects.filter() # django example, or in sqlalchemy: session.query(Transaction).filter()
  561. with alive_bar(qs.count()) as bar:
  562. for transaction in qs:
  563. if faulty(transaction):
  564. with bar.pause():
  565. yield transaction
  566. bar()
  567. ```
  568. That's it! It's that simple! \o/
  569. <br>Now run `gen = reconcile_transactions()` to instantiate the generator, and whenever you want the next faulty transaction, just call `next(gen, None)`! I love it...
  570. <br>The `alive-progress` bar will start and run as usual, but as soon as any inconsistency is found, the bar will pause itself, turning off the refresh thread and remembering its exact state, and yield the transaction to you directly on the prompt! It's almost magic! 😃
  571. ```text
  572. In [11]: gen = reconcile_transactions()
  573. In [12]: next(gen, None)
  574. |█████████████████████ | 105/200 [52%] in 5s (18.8/s, eta: 4s)
  575. Out[12]: Transaction<#123>
  576. ```
  577. You can then inspect the transaction with the usual `_` shortcut of `ipython` (or just directly assign it with `t = next(gen, None)`), and you're all set to fix it!
  578. <br>When you're done, just reactivate the bar with the same `next` call as before!! The bar reappears, turns everything back on, and continues **like it had never stopped**!! Ok, it is magic 😜
  579. ```text
  580. In [21]: next(gen, None)
  581. |█████████████████████ | ▁▃▅ 106/200 [52%] in 5s (18.8/s, eta: 4s)
  582. ```
  583. Rinse and repeat till the final receipt appears, and there'll be no faulty transactions anymore. 😄
  584. ### Loop-less use
  585. So, you need to monitor a fixed operation, without any loops, right?
  586. <br>It'll work for sure! Here is a naive example (we'll do better in a moment):
  587. ```python
  588. with alive_bar(4) as bar:
  589. corpus = read_file(file)
  590. bar() # file was read, tokenizing
  591. tokens = tokenize(corpus)
  592. bar() # tokens generated, processing
  593. data = process(tokens)
  594. bar() # process finished, sending response
  595. resp = send(data)
  596. bar() # we're done! four bar calls with `total=4`
  597. ```
  598. It's naive because it assumes all steps take the same amount of time, but actually, each one may take a very different time to complete. Think `read_file` and `tokenize` may be extremely fast, which makes the percentage skyrocket to 50%, then stopping for a long time in the `process` step... You get the point, it can ruin the user experience and create a very misleading ETA.
  599. To improve upon that you need to distribute the steps' percentages accordingly! Since you told `alive_bar` there were four steps, when the first one was completed it understood 1/4 or 25% of the whole processing was complete... Thus, you need to measure how long your steps actually take and use the **manual mode** to increase the bar percentage by the right amount at each step!
  600. You can use my other open source project [about-time](https://github.com/rsalmei/about-time) to easily measure these durations! Just try to simulate with some representative inputs, to get better results. Something like:
  601. ```python
  602. from about_time import about_time
  603. with about_time() as t_total: # this about_time will measure the whole time of the block.
  604. with about_time() as t1: # the other four will get the relative timings within the whole.
  605. corpus = read_file(file) # `about_time` supports several calling conventions, including one-liners.
  606. with about_time() as t2: # see its documentation for more details.
  607. tokens = tokenize(corpus)
  608. with about_time() as t3:
  609. data = process(tokens)
  610. with about_time() as t4:
  611. resp = send(data)
  612. print(f'percentage1 = {t1.duration / t_total.duration}')
  613. print(f'percentage2 = {t2.duration / t_total.duration}')
  614. print(f'percentage3 = {t3.duration / t_total.duration}')
  615. print(f'percentage4 = {t4.duration / t_total.duration}')
  616. ```
  617. There you go! Now you know the relative timings of all the steps, and can use them to improve your original code! Just get the cumulative timings and put them within a manual mode `alive_bar`!
  618. For example, if the timings you found were 10%, 30%, 20%, and 40%, you'd use 0.1, 0.4, 0.6, and 1.0 (the last one should always be 1.0):
  619. ```python
  620. with alive_bar(4, manual=True) as bar:
  621. corpus = read_big_file()
  622. bar(0.1) # 10%
  623. tokens = tokenize(corpus)
  624. bar(0.4) # 30% + 10% from previous steps
  625. data = process(tokens)
  626. bar(0.6) # 20% + 40% from previous steps
  627. resp = send(data)
  628. bar(1.) # always 1. in the last step
  629. ```
  630. That's it! The user experience and ETA should be greatly improved now.
  631. ### FPS Calibration
  632. Yes, you can calibrate the spinner speed!
  633. The `alive-progress` bars have cool visual feedback of the current throughput, so you can actually **see** how fast your processing is, as the spinner runs faster or slower with it.
  634. <br>For this to happen, I've put together and implemented a few fps curves to empirically find which one gave the best feel of speed:
  635. <p align="center"><img alt="alive-progress fps curves" src="https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-bar_fps.png" width="80%" height="80%"></p>
  636. <p align="center">(interactive version [here](https://www.desmos.com/calculator/ema05elsux))</p>
  637. The graph shows the logarithmic (red), parabolic (blue) and linear (green) curves, these are the ones I started with. It was not an easy task, I've made dozens of tests, and never found one that really inspired that feel of speed I was looking for. The best one seemed to be the logarithmic one, but it reacted poorly with small numbers.
  638. I know I could make it work with a few twists for those small numbers, so I experimented a lot and adjusted the logarithmic curve (dotted orange) until I finally found the behavior I expected! It is the one that seemed to provide the best all-around perceived speed changes throughout the whole spectrum from a few to billions...
  639. That is the curve I've settled with, and it's the one used in all modes and conditions. In the future and if someone would find it useful, that curve could be configurable.
  640. Well, the default `alive-progress` calibration is **1,000,000** in bounded modes, i.e., it takes 1 million iterations per second for the bar to refresh itself at 60 frames per second. In the manual unbounded mode, it is **1.0** (100%). Both enable a vast operating range and generally work quite well.
  641. For example, take a look at the effect these very different calibrations have, running the very same code at the very same speed! Notice the feel the spinner passes to the user, is this processing going slow or going fast? And remember that isn't only the spinner refreshing but the whole line, complete with the bar rendition and all widgets, so everything gets smoother or sluggish:
  642. ![alive-progress calibration](https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/alive-calibration.gif)
  643. > So, if your processing hardly gets to 20 items per second, and you think `alive-progress` is rendering sluggish, you could increase that sense of speed by calibrating it to let's say `40`, and it will be running waaaay faster... It is better to always leave some headroom and calibrate it to something between 50% and 100% more, and then tweak it from there to find the one you like the most! :)
  644. ### Forcing animations on PyCharm, Jupyter, etc.
  645. Do these astonishing `alive-progress` animations refuse to display?
  646. PyCharm is awesome, I love it! But I'll never understand why they've disabled emulating a terminal by default... If you do use PyCharm's output console, please enable this on all your Run Configurations:
  647. <p align="center"><img alt="alive-progress in pycharm" src="https://raw.githubusercontent.com/rsalmei/alive-progress/main/img/pycharm-terminal.png"></p>
  648. > I even recommend you go into `File` > `New Projects Setup` > `Run Configuration Templates`, select `Python`, and also enable it there, so any new ones you create will already have this set.
  649. In addition to that, some terminals report themselves as "non-interactive", like when running out of a real terminal (PyCharm and Jupyter for example), in shell pipelines (`cat file.txt | python program.py`), or in background processes (not connected to a tty).
  650. When `alive-progress` finds itself in a non-interactive terminal, it automatically disables all kinds of animations, printing only the final receipt. This is made in order to avoid both messing up the pipeline output and spamming your log file with thousands of `alive-progress` refreshes.
  651. So, when you know it's safe, you can force them to see `alive-progress` in all its glory! Here is the `force_tty` argument:
  652. ```python
  653. with alive_bar(1000, force_tty=True) as bar:
  654. for i in range(1000):
  655. time.sleep(.01)
  656. bar()
  657. ```
  658. The values accepted are:
  659. - `force_tty=True` -> always enables animations, and auto-detects Jupyter Notebooks!
  660. - `force_tty=False` -> always disables animations, keeping only the final receipt
  661. - `force_tty=None` (default) -> auto detect, according to the terminal's tty state
  662. You can also set it system-wide using `config_handler`, so you don't need to pass it manually anymore.
  663. > Do note that PyCharm's console and Jupyter notebooks are heavily instrumented and thus have much more overhead, so the outcome may not be as fluid as you would expect. On top of that, Jupyter notebooks do not support ANSI Escape Codes, so I had to develop some workarounds to emulate functions like "clear the line" and "clear from cursor"... To see the fluid and smooth `alive_bar` animations as I intended, always prefer a full-fledged terminal.
  664. ## Interesting facts
  665. - This whole project was implemented in functional style;
  666. - It uses extensively (and very creatively) Python _Closures_ and _Generators_, e.g. all [spinners](https://github.com/rsalmei/alive-progress/blob/main/alive_progress/animations/spinners.py#L10) are made with cool _Generator Expressions_! Besides it, there are other cool examples like the [exhibit](https://github.com/rsalmei/alive-progress/blob/main/alive_progress/styles/exhibit.py#L42) module, and the core [spinner player](https://github.com/rsalmei/alive-progress/blob/main/alive_progress/animations/utils.py#L10-L17)/[spinner runner](https://github.com/rsalmei/alive-progress/blob/main/alive_progress/animations/spinner_compiler.py#L233) generators; 😜
  667. - Until 2.0, `alive-progress` hadn't had any dependency. Now it has two: one is [about-time](https://github.com/rsalmei/about-time) (another interesting project of mine, if I say so myself), which is used to track the time it takes for the spinner compilation, and to generate its human-friendly renditions. The other is [grapheme](https://github.com/alvinlindstam/grapheme), to detect grapheme cluster breaks (I've opened an [issue](https://github.com/alvinlindstam/grapheme/issues/13) there asking about the future and correctness of it, and the author guarantees he intends to update the project on every new Unicode version);
  668. - Also, until 2.0, `alive-progress` hadn't had a single Python class! Now it has a few tiny ones for very specific reasons (change callables, iterator adapters, and some descriptors for the `alive_bar` widgets).
  669. - Everything else is either a function or a closure, which generate other closures internally with some state on the parent context. I've used them to create spinner factories, bar factories, the global configuration, the system hooks, the spinner compiler (which is also a big _Function Decorator_), etc.! Even `alive_bar` itself is just a function! Although, to be fair, it is "just" a function where I dynamically plug several closures from within into itself (remember that Python functions have a `__dict__` just like classes do 😝).
  670. ## To do
  671. - enable multiple simultaneous bars for nested or multiple activities (the most requested feature, but very complex).
  672. - reset a running bar context, i.e. run in unknown mode while "quantifying" the work, then switch to the auto mode.
  673. - dynamic bar width rendition, which notices terminal size changes and shrink or expand the bar as needed (currently `alive_bar` does notice terminal size changes, but just truncates the line accordingly).
  674. - improve test coverage, currently at 87% branch coverage (but it's very hard since it's multithreaded, full of stateful closures, and includes system print hooks).
  675. - create a `contrib` system somehow, to allow a simple way to share cool spinners and bars from users.
  676. - support colors in spinners and bars (it's very hard, since color codes alter string sizes, which makes it tricky to synchronize animations and correctly slicing, reversing, and iterating fragments of strings while _still keeping color codes_--which is very, very complex) --> probably simpler now with the new _Cell Architecture_.
  677. - any other ideas are welcome!
  678. <details>
  679. <summary>Noteworthy features already done ✅</summary>
  680. - resuming computations support with `skipped` items
  681. - help system on configuration errors
  682. - readable widgets to extract information
  683. - exponential smoothing algorithm for the rate
  684. - support for using `stderr` and other files instead of `stdout`
  685. - units with automatic scaling
  686. - dual-line mode
  687. - customize final receipt widgets
  688. - customize widgets rendition like `monitor`, `elapsed`, `stats`
  689. - bar title can be dynamically set, changed or removed
  690. - exponential smoothing algorithm for the ETA
  691. - jupyter notebook support, which works the same as in the terminal, animations and everything
  692. - create an unknown mode for bars (without a known total and eta)
  693. - implement a pausing mechanism
  694. - change spinner styles
  695. - change bar styles
  696. - include a global configuration system
  697. - create customizable generators for scrolling, bouncing, delayed, and compound spinners
  698. - create an exhibition for spinners and bars, to see them all in motion
  699. - include theme support in configuration
  700. - soft wrapping support
  701. - hiding cursor support
  702. - Python logging support
  703. - exponential smoothing of ETA time series
  704. - create an exhibition for themes
  705. </details>
  706. <details>
  707. <summary>Changelog highlights</summary>
  708. <br>Complete [here](https://github.com/rsalmei/alive-progress/blob/main/CHANGELOG.md).
  709. - 3.3.0: new bar.receipt and bar.elapsed; allow changing bar title and text even after finished; use `graphemeu` dependency; add `py.typed`; include Python 3.14 support in CI
  710. - 3.2.0: print/logging hooks now support multithreading, rounded ETAs for long tasks, support for zero and negative bar increments, custom offset for enriched print/logging messages, improved compatibility with PyInstaller and Celery, drop 3.7 and 3.8, hello 3.12 and 3.13
  711. - 3.1.4: support spaces at the start and end of titles and units
  712. - 3.1.3: better error handling of invalid `alive_it` calls, detect nested uses of alive_progress and throw a clearer error message
  713. - 3.1.2: fix some exotic ANSI Escape Codes not being printed (OSC)
  714. - 3.1.1: support for printing ANSI Escape Codes without extra newlines, typing annotations in `alive_it`
  715. - 3.1.0: new resuming computations support with `skipped` items, new `max_cols` config setting for jupyter, fix fetching the size of the terminal when using stderr, officially supports Python 3.11
  716. - 3.0.1: fix for logging streams that extend StreamHandler but doesn't allow changing streams
  717. - 3.0.0: units support with automatic and configurable scaling and precision, automatic stats scaling for slow throughputs, support for using `sys.stderr` and other files instead of `sys.stdout`, smoothed out the rate estimation, more queries into the currently running widgets' data, help system in configuration errors
  718. - 2.4.1: fix a crash when dual-line and disabled are set
  719. - 2.4.0: support dual line text mode; finalize function parameter in alive_it; improve logging support, detecting customized ones
  720. - 2.3.1: introduce ctrl_c config param; print the final receipt even when interrupted
  721. - 2.3.0: customizable `monitor`, `elapsed`, and `stats` core widgets, new `monitor_end`, `elapsed_end`, and `stats_end` core widgets, better support for CTRL+C, which makes `alive_bar` stop prematurely
  722. - 2.2.0: bar title can be dynamically set, changed or removed; customizable refresh rates; final receipt can be hidden; `click.echo()` support; faster performance; safer detection of terminal columns; `bar.current` acts like a property; remove Python 3.6
  723. - 2.1.0: Jupyter notebook support (experimental), Jupyter auto-detection, disable feature and configuration
  724. - 2.0.0: new system-wide Cell Architecture with grapheme clusters support; super cool spinner compiler and runner; `.check()` tools in both spinners and bars; bars and spinners engines revamp; new animation modes in alongside and sequential spinners; new builtin spinners, bars, and themes; dynamic showtime with themes, scroll protection and filter patterns; improved logging for files; several new configuration options for customizing appearance; new iterator adapter `alive_it`; uses `time.perf_counter()` high-resolution clock; requires Python 3.6+ (and officially supports Python 3.9 and 3.10)
  725. - 1.6.2: new `bar.current()` method; newlines get printed on vanilla Python REPL; the bar is truncated to 80 chars on Windows
  726. - 1.6.1: fix logging support for Python 3.6 and lower; support logging for file; support for wide Unicode chars, which use 2 columns but have length 1
  727. - 1.6.0: soft wrapping support; hiding cursor support; Python logging support; exponential smoothing of ETA time series; proper bar title, always visible; enhanced times representation; new `bar.text()` method, to set situational messages at any time, without incrementing position (deprecates 'text' parameter in `bar()`); performance optimizations
  728. - 1.5.1: fix compatibility with Python 2.7 (should be the last one, version 2 is in the works, with Python 3 support only)
  729. - 1.5.0: standard_bar accepts a `background` parameter instead of `blank`, which accepts arbitrarily sized strings and remains fixed in the background, simulating a bar going "over it"
  730. - 1.4.4: restructure internal packages; 100% branch coverage of all animations systems, i.e., bars and spinners
  731. - 1.4.3: protect configuration system against other errors (length='a' for example); first automated tests, 100% branch coverage of configuration system
  732. - 1.4.2: sanitize text input, keeping \n from entering and replicating bar on the screen
  733. - 1.4.1: include license file in the source distribution
  734. - 1.4.0: print() enrichment can now be disabled (locally and globally), exhibits now have a real-time fps indicator, new exhibit functions `show_spinners` and `show_bars`, new utility `print_chars`, `show_bars` gain some advanced demonstrations (try it again!)
  735. - 1.3.3: further improve stream compatibility with isatty
  736. - 1.3.2: beautifully finalize bar in case of unexpected errors
  737. - 1.3.1: fix a subtle race condition that could leave artifacts if ended very fast, flush print buffer when position changes or bar terminates, keep the total argument from unexpected types
  738. - 1.3.0: new fps calibration system, support force_tty and manual options in global configuration, multiple increment support in bar handler
  739. - 1.2.0: filled blanks bar styles, clean underflow representation of filled blanks
  740. - 1.1.1: optional percentage in manual mode
  741. - 1.1.0: new manual mode
  742. - 1.0.1: pycharm console support with force_tty, improve compatibility with Python stdio streams
  743. - 1.0.0: first public release, already very complete and mature
  744. </details>
  745. ## Python End of Life notice
  746. `alive_progress` will always try to keep up with Python, so starting from version 2.0, I'll drop support for all Python versions which enter EoL. See their schedule [here](https://endoflife.date/python).
  747. But don't worry if you can't migrate just yet: `alive_progress` versions are perennial, so just keep using the one that works for you and you're good.
  748. <br>I just strongly recommend setting older `alive_progress` packages in a requirements.txt file with the following formats. These will always fetch the latest build releases previous to a given version, so, if I ever release bug fixes, you'll get them too.
  749. ### For Python 2.7 and 3.5
  750. ```sh
  751. ❯ pip install -U "alive_progress<2"
  752. ```
  753. ### For Python 3.6
  754. ```sh
  755. ❯ pip install -U "alive_progress<2.2"
  756. ```
  757. ### For Python 3.7 and 3.8
  758. ```sh
  759. ❯ pip install -U "alive_progress<3.2"
  760. ```
  761. ## License
  762. This software is licensed under the MIT License. See the LICENSE file in the top distribution directory for the full license text.
  763. ---
  764. Maintaining an open source project is hard and time-consuming, and I've put much ❤️ and effort into this.
  765. If you've appreciated my work, you can back me up with a donation! Thank you 😊
  766. [<img align="right" src="https://cdn.buymeacoffee.com/buttons/default-orange.png" width="217px" height="51x">](https://www.buymeacoffee.com/rsalmei)
  767. [<img align="right" alt="Donate with PayPal button" src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif">](https://www.paypal.com/donate?business=6SWSHEB5ZNS5N&no_recurring=0&item_name=I%27m+the+author+of+alive-progress%2C+clearly+and+about-time.+Thank+you+for+appreciating+my+work%21&currency_code=USD)
  768. ---