1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
[](https://travis-ci.org/rasbt/pyprind)
[](https://coveralls.io/github/rasbt/pyprind?branch=master)


[](https://github.com/rasbt/pyprind/blob/master/LICENSE.txt)
# PyPrind (Python Progress Indicator)
The `PyPrind` (Python Progress Indicator) module provides a **progress bar** and a
**percentage indicator** object that let you track the progress of a loop structure or other iterative computation.
Typical applications include the processing of large data sets to provide an intuitive estimate
at runtime about the progress of the computation.

<br>
#### Progress Bars and Percentage Generators
```python
import pyprind
for i in pyprind.prog_bar(range(n)):
time.sleep(timesleep) # your computation here
```
```
0% 100%
[##############################] | ETA: 00:00:00
Total time elapsed: 00:00:05
```
<br>
<br>
```python
for i in pyprind.prog_percent(range(n)):
time.sleep(timesleep) # your computation here
```
```
[10 %] Time elapsed: 00:00:01 | ETA: 00:00:04
```
#### While-loops
The `ProgBar` and `ProgPercent` classes also support while loops if desired.
The objects are updated inside the loop using the `update` method as shown below:
```python
import random
import pyprind
import time
timesleep = 0.05
random.seed(1)
collection = set()
n = 100
bar = pyprind.ProgBar(n, track_time=False, title='while example')
while len(collection) < n:
r = random.randint(0, 10**5)
if r % 7 and r not in collection:
collection.add(r)
bar.update()
time.sleep(timesleep)
print(bar)
```
```
while example
0% 100%
[##############################]
Title: while example
Started: 09/07/2016 13:06:58
Finished: 09/07/2016 13:07:03
Total time elapsed: 00:00:05
```
<br>
<br>
<a id='advanced_tracking'>
#### Advanced Tracking
If you have the `psutil` package installed, you can set the `monitor=True` to track CPU and memory usage:
```python
bar = pyprind.ProgBar(n, monitor=True)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
print(bar)
```
```
0% 100%
[##############################] | ETA: 00:00:00
Total time elapsed: 00:00:05
Title:
Started: 09/07/2016 13:14:09
Finished: 09/07/2016 13:14:14
Total time elapsed: 00:00:05
CPU %: 1.90
Memory %: 0.48
```
<br>
<br>
#### Choose Your Favorite Bar Style
```python
bar = pyprind.ProgBar(n, bar_char='█')
for i in range(n):
time.sleep(0.1) # do some computation
bar.update()
```
```
0% 100%
[██████████████████████████████] | ETA: 00:00:00
Total time elapsed: 00:00:10
```
#### Note to PyCharm users
If you are using the PyCharm IDE, you need to pass the `sys.stdout` or `sys.err`
as a `stream` argument to display the progress indicators correctly in the IDE. For example,
```python
import sys
bar = pyprind.ProgBar(n, stream=sys.stdout)
for i in range(n):
time.sleep(0.1) # do some computation
bar.update()
```
<br>
<br>
###[View more examples in an IPython Demo Notebook](http://nbviewer.ipython.org/github/rasbt/pyprind/blob/master/examples/pyprind_demo.ipynb)
<br>
<br>
<a id='sections'>
# Sections
- [Installation](#installation)
- [Documentation](#documentation)
- [Examples](#examples)
- [Contact](#contact)
- [Changelog](https://raw.githubusercontent.com/rasbt/pyprind/master/CHANGELOG.txt)
<p><a id="installation"></a></p>
<br>
<br>
<br>
# Installation
[[back to top](#sections)]
The most convenient way to install PyPrind is via tools like `pip` or `easy_install`:
- `pip install pyprind`
- `easy_install pyprind`
PyPrind comes without any dependencies except for the optional [psutil](https://pypi.python.org/pypi/psutil) to [monitor CPU and memory usages](#advanced_tracking). You can install `psutil` via `pip install psutil` or install it automatically with PyPrind:
`pip install pyprind -r requirements.txt`
Alternatively, you can install PyPrind the classic way: Download the package from the Python Package Index [https://pypi.python.org/pypi/PyPrind](https://pypi.python.org/pypi/PyPrind), unzip it,
navigate into the unzipped directory, and use the command
`python setup.py install`
<p><a id="documentation"></a></p>
<br>
<br>
<br>
# Documentation
[[back to top](#sections)]
PyPrind consists of two class objects that can visualize the progress of a computation on the output screen.
Progress bars are available via `ProgBar`, and percentage indicators can be used via a `ProgPercent`.
```python
n = 10000000
bar = pyprind.ProgBar(n) # 1) initialization with number of iterations
for i in range(n):
# do some computation
bar.update() # 2) update the progress visualization
```
Alternatively, the progress can be tracked via the equivalent generator functions `prog_bar` and `prog_percent`:
```python
for i in pyprind.prog_bar(range(n)):
# do something
pass
```
<br>
#### Complete Parameter of Parameters and Options
##### ProgBar
*`ProgBar(iterations, track_time=True, width=30, bar_char='#',
stream=2, title='', monitor=False, update_interval=None))`*
- iterations : `int`
Number of iterations for the iterative computation.
- track_time : `bool` (default: `True`)
Prints elapsed time when loop has finished.
- width : `int` (default: 30)
Sets the progress bar width in characters.
- stream : `int` (default: 2).
Setting the output stream.
Takes `1` for stdout, `2` for stderr, or a custom stream object
- title : `str` (default: `''`)
Setting a title for the progress bar.
- monitor : `bool` (default: `False`)
Monitors CPU and memory usage if `True` (requires `psutil` package).
- update_interval : float or int (default: `None`)
The update_interval in seconds controls how often the progress
is flushed to the screen.
Automatic mode if `update_interval=None`.
##### ProgPercent
*`ProgPercent(iterations, track_time=True,
stream=2, title='', monitor=False, update_interval=None)`*
- iterations : `int`
Number of iterations for the iterative computation.
- track_time : `bool` (default: `True`)
Prints elapsed time when loop has finished.
- stream : `int` (default: 2).
Setting the output stream.
Takes `1` for stdout, `2` for stderr, or a custom stream object
- title : `str` (default : `''`).
Setting a title for the percentage indicator.
- monitor : `bool` (default: `False`)
Monitors CPU and memory usage if `True` (requires `psutil` package).
- update_interval : float or int (default: `None`)
The update_interval in seconds controls how often the progress
is flushed to the screen.
Automatic mode if `update_interval=None`.
##### update method
*`update(iterations=1, item_id=None, force_flush=False)`*
- iterations : int (default: `1`)
default argument can be changed to integer values
`>=1` in order to update the progress indicators more than once
per iteration.
- item_id : str (default: `None`)
Print an item_id sring behind the progress bar
- force_flush : bool (default: `False`)
If True, flushes the progress indicator to the output screen
in each iteration.
<br>
<p><a id="examples"></a></p>
<br>
<br>
<br>
# Examples
[[back to top](#sections)]
Examples for using the progress bar and percentage indicator objects can be found in the [IPython Demo Notebook](https://github.com/rasbt/pyprind/blob/master/examples/pyprind_demo.ipynb).
<p><a id="contact"></a></p>
<br>
<br>
<br>
|