# fastprogress
A fast and simple progress bar for Jupyter Notebook and console. Created by Sylvain Gugger for fast.ai.
<img src="https://github.com/fastai/fastprogress/raw/master/images/cifar_train.gif" width="600">
## Install
To install simply use
```
pip install fastprogress
```
or:
```
conda install -c fastai fastprogress
```
Note that this requires python 3.6 or later.
## Usage
### Example 1
Here is a simple example. Each bar takes an iterator as a main argument, and we can specify the second bar is nested with the first by adding the argument `parent=mb`. We can then:
- add a comment in the first bar by changing the value of `mb.main_bar.comment`
- add a comment in the second bar by changing the value of `mb.child.comment`
- write a line between the two bars with `mb.write('message')`
``` python
from fastprogress.fastprogress import master_bar, progress_bar
from time import sleep
mb = master_bar(range(10))
for i in mb:
for j in progress_bar(range(100), parent=mb):
sleep(0.01)
mb.child.comment = f'second bar stat'
mb.main_bar.comment = f'first bar stat'
mb.write(f'Finished loop {i}.')
#mb.update_graph(graphs, x_bounds, y_bounds)
```
<img src="https://github.com/fastai/fastprogress/raw/master/images/pb_basic.gif" width="600">
### Example 2
To add a graph that get plots as the training goes, just use the command `mb.update_graphs`. It will create the figure on its first use. Arguments are:
- `graphs`: a list of graphs to be plotted (each of the form `[x,y]`)
- `x_bounds`: the min and max values of the x axis (if `None`, it will those given by the graphs)
- `y_bounds`: the min and max values of the y axis (if `None`, it will those given by the graphs)
Note that it's best to specify `x_bounds` and `y_bounds`, otherwise the box will change as the loop progresses.
Additionally, we can give the label of each graph via the command `mb.names` (should have as many elements as the graphs argument).
``` python
import numpy as np
mb = master_bar(range(10))
mb.names = ['cos', 'sin']
for i in mb:
for j in progress_bar(range(100), parent=mb):
if j%10 == 0:
k = 100 * i + j
x = np.arange(0, 2*k*np.pi/1000, 0.01)
y1, y2 = np.cos(x), np.sin(x)
graphs = [[x,y1], [x,y2]]
x_bounds = [0, 2*np.pi]
y_bounds = [-1,1]
mb.update_graph(graphs, x_bounds, y_bounds)
mb.child.comment = f'second bar stat'
mb.main_bar.comment = f'first bar stat'
mb.write(f'Finished loop {i}.')
```
<img src="https://github.com/fastai/fastprogress/raw/master/images/pb_cos.gif" width="600">
Here is the rendering in console:
<img src="https://github.com/fastai/fastprogress/raw/master/images/pb_console.gif" width="800">
If the script using this is executed with a redirect to a file, only the results of the `.write` method will be printed in that file.
### Example 3
Here is an example that a typical machine learning training loop can use. It also demonstrates how to set `y_bounds` dynamically.
```
def plot_loss_update(epoch, epochs, mb, train_loss, valid_loss):
""" dynamically print the loss plot during the training/validation loop.
expects epoch to start from 1.
"""
x = range(1, epoch+1)
y = np.concatenate((train_loss, valid_loss))
graphs = [[x,train_loss], [x,valid_loss]]
x_margin = 0.2
y_margin = 0.05
x_bounds = [1-x_margin, epochs+x_margin]
y_bounds = [np.min(y)-y_margin, np.max(y)+y_margin]
mb.update_graph(graphs, x_bounds, y_bounds)
```
And here is an emulation of a training loop that uses this function:
```
from fastprogress.fastprogress import master_bar, progress_bar
from time import sleep
import numpy as np
import random
epochs = 5
mb = master_bar(range(1, epochs+1))
# optional: graph legend: if not set, the default is 'train'/'valid'
# mb.names = ['first', 'second']
train_loss, valid_loss = [], []
for epoch in mb:
# emulate train sub-loop
for batch in progress_bar(range(2), parent=mb): sleep(0.2)
train_loss.append(0.5 - 0.06 * epoch + random.uniform(0, 0.04))
# emulate validation sub-loop
for batch in progress_bar(range(2), parent=mb): sleep(0.2)
valid_loss.append(0.5 - 0.03 * epoch + random.uniform(0, 0.04))
plot_loss_update(epoch, epochs, mb, train_loss, valid_loss)
```
And the output:
<img src="https://github.com/fastai/fastprogress/raw/master/images/pb_graph.gif" alt="Output">
----
Copyright 2017 onwards, fast.ai. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. A copy of the License is provided in the LICENSE file in this repository.
Raw data
{
"_id": null,
"home_page": "https://github.com/fastai/fastprogress",
"name": "fastprogress",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": "",
"keywords": "jupyter notebook progressbar",
"author": "Sylvain Gugger",
"author_email": "info@fast.ai",
"download_url": "https://files.pythonhosted.org/packages/95/52/77c827febdf8aac3b0840ce0edb7c34cbd330fab9bbe032c2a79ec8f2d51/fastprogress-1.0.3.tar.gz",
"platform": null,
"description": "# fastprogress\n\nA fast and simple progress bar for Jupyter Notebook and console. Created by Sylvain Gugger for fast.ai.\n\n<img src=\"https://github.com/fastai/fastprogress/raw/master/images/cifar_train.gif\" width=\"600\">\n\n## Install\n\nTo install simply use\n```\npip install fastprogress\n```\nor:\n```\nconda install -c fastai fastprogress\n```\nNote that this requires python 3.6 or later.\n\n## Usage\n\n### Example 1\n\nHere is a simple example. Each bar takes an iterator as a main argument, and we can specify the second bar is nested with the first by adding the argument `parent=mb`. We can then:\n- add a comment in the first bar by changing the value of `mb.main_bar.comment`\n- add a comment in the second bar by changing the value of `mb.child.comment`\n- write a line between the two bars with `mb.write('message')`\n\n``` python\nfrom fastprogress.fastprogress import master_bar, progress_bar\nfrom time import sleep\nmb = master_bar(range(10))\nfor i in mb:\n for j in progress_bar(range(100), parent=mb):\n sleep(0.01)\n mb.child.comment = f'second bar stat'\n mb.main_bar.comment = f'first bar stat'\n mb.write(f'Finished loop {i}.')\n #mb.update_graph(graphs, x_bounds, y_bounds)\n```\n\n<img src=\"https://github.com/fastai/fastprogress/raw/master/images/pb_basic.gif\" width=\"600\">\n\n### Example 2\n\nTo add a graph that get plots as the training goes, just use the command `mb.update_graphs`. It will create the figure on its first use. Arguments are:\n- `graphs`: a list of graphs to be plotted (each of the form `[x,y]`)\n- `x_bounds`: the min and max values of the x axis (if `None`, it will those given by the graphs)\n- `y_bounds`: the min and max values of the y axis (if `None`, it will those given by the graphs)\n\nNote that it's best to specify `x_bounds` and `y_bounds`, otherwise the box will change as the loop progresses.\n\nAdditionally, we can give the label of each graph via the command `mb.names` (should have as many elements as the graphs argument).\n\n``` python\nimport numpy as np\nmb = master_bar(range(10))\nmb.names = ['cos', 'sin']\nfor i in mb:\n for j in progress_bar(range(100), parent=mb):\n if j%10 == 0:\n k = 100 * i + j\n x = np.arange(0, 2*k*np.pi/1000, 0.01)\n y1, y2 = np.cos(x), np.sin(x)\n graphs = [[x,y1], [x,y2]]\n x_bounds = [0, 2*np.pi]\n y_bounds = [-1,1]\n mb.update_graph(graphs, x_bounds, y_bounds)\n mb.child.comment = f'second bar stat'\n mb.main_bar.comment = f'first bar stat'\n mb.write(f'Finished loop {i}.')\n```\n\n<img src=\"https://github.com/fastai/fastprogress/raw/master/images/pb_cos.gif\" width=\"600\">\n\nHere is the rendering in console:\n\n<img src=\"https://github.com/fastai/fastprogress/raw/master/images/pb_console.gif\" width=\"800\">\n\nIf the script using this is executed with a redirect to a file, only the results of the `.write` method will be printed in that file.\n\n### Example 3\n\nHere is an example that a typical machine learning training loop can use. It also demonstrates how to set `y_bounds` dynamically.\n\n```\ndef plot_loss_update(epoch, epochs, mb, train_loss, valid_loss):\n \"\"\" dynamically print the loss plot during the training/validation loop.\n expects epoch to start from 1.\n \"\"\"\n x = range(1, epoch+1)\n y = np.concatenate((train_loss, valid_loss))\n graphs = [[x,train_loss], [x,valid_loss]]\n x_margin = 0.2\n y_margin = 0.05\n x_bounds = [1-x_margin, epochs+x_margin]\n y_bounds = [np.min(y)-y_margin, np.max(y)+y_margin]\n\n mb.update_graph(graphs, x_bounds, y_bounds)\n```\n\nAnd here is an emulation of a training loop that uses this function:\n\n```\nfrom fastprogress.fastprogress import master_bar, progress_bar\nfrom time import sleep\nimport numpy as np\nimport random\n\nepochs = 5\nmb = master_bar(range(1, epochs+1))\n# optional: graph legend: if not set, the default is 'train'/'valid'\n# mb.names = ['first', 'second']\ntrain_loss, valid_loss = [], []\nfor epoch in mb:\n # emulate train sub-loop\n for batch in progress_bar(range(2), parent=mb): sleep(0.2)\n train_loss.append(0.5 - 0.06 * epoch + random.uniform(0, 0.04))\n\n # emulate validation sub-loop\n for batch in progress_bar(range(2), parent=mb): sleep(0.2)\n valid_loss.append(0.5 - 0.03 * epoch + random.uniform(0, 0.04))\n\n plot_loss_update(epoch, epochs, mb, train_loss, valid_loss)\n```\n\nAnd the output:\n\n<img src=\"https://github.com/fastai/fastprogress/raw/master/images/pb_graph.gif\" alt=\"Output\">\n\n----\n\nCopyright 2017 onwards, fast.ai. Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. A copy of the License is provided in the LICENSE file in this repository.\n\n\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "A nested progress with plotting options for fastai",
"version": "1.0.3",
"split_keywords": [
"jupyter",
"notebook",
"progressbar"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "e1b627a94b00f87d54f766b74427ca32",
"sha256": "6dfea88f7a4717b0a8d6ee2048beae5dbed369f932a368c5dd9caff34796f7c5"
},
"downloads": -1,
"filename": "fastprogress-1.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e1b627a94b00f87d54f766b74427ca32",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 12744,
"upload_time": "2022-07-22T07:02:12",
"upload_time_iso_8601": "2022-07-22T07:02:12.547118Z",
"url": "https://files.pythonhosted.org/packages/a7/8f/213223fdee199c55db81e2d0c669f30e8285c5be2526c4ed924de39247da/fastprogress-1.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "51662ddeb3429d926b113da6a2d049e4",
"sha256": "7a17d2b438890f838c048eefce32c4ded47197ecc8ea042cecc33d3deb8022f5"
},
"downloads": -1,
"filename": "fastprogress-1.0.3.tar.gz",
"has_sig": false,
"md5_digest": "51662ddeb3429d926b113da6a2d049e4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 14920,
"upload_time": "2022-07-22T07:02:14",
"upload_time_iso_8601": "2022-07-22T07:02:14.983654Z",
"url": "https://files.pythonhosted.org/packages/95/52/77c827febdf8aac3b0840ce0edb7c34cbd330fab9bbe032c2a79ec8f2d51/fastprogress-1.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-07-22 07:02:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "fastai",
"github_project": "fastprogress",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fastprogress"
}