{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[Sebastian Raschka](http://sebastianraschka.com)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Last updated: 2021-04-18\n", "\n", "Python implementation: CPython\n", "Python version : 3.9.2\n", "IPython version : 7.21.0\n", "\n", "pyprind: 2.11.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -d -u -p pyprind" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# PyPrind demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sections" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Basic Progress Bar](#Basic-Progress-Bar) \n", "- [Basic Percentage Indicator](#Basic-Percentage-Indicator) \n", "- [Progress Bar and Percentage Indicator generators](#Progress-Bar-and-Percentage-Indicator-generators)\n", "- [Progress Bar/Percentage Indicator - Reporting tracking information](#Progress-Bar/Percentage-Indicator---Reporting-CPU-and-memory-usage)\n", "- [Progress Bar/Percentage Indicator - Reporting CPU and memory usage](#Progress-Bar/Percentage-Indicator---Reporting-CPU-and-memory-usage) \n", "- [Progress Bar/Percentage Indicator - Setting a title](#Progress-Bar/Percentage-Indicator---Setting-a-title)\n", "- [Progress Bar - Changing the default width](#Progress-Bar---Changing-the-default-width)\n", "- [Progress Bar/Percentage Indicator - Changing the output stream](#Progress-Bar/Percentage-Indicator---Setting-a-title)\n", "- [Stopping the Progress Bar/Percentage Indicator early](#Stopping-the-Progress-Bar/Percentage-Indicator-early)\n", "- [Choosing your own progress bar style](#Choosing-your-own-progress-bar-style)\n", "- [Controlling the update frequency](#Controlling-the-update-frequency)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import pyprind" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Progress Bar" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Using a for-loop**" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "import time\n", "\n", "n = 100\n", "timesleep = 0.05\n", "\n", "bar = pyprind.ProgBar(n)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**In a while-loop**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "while example\n", "0% [##############################] 100%\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Title: while example\n", " Started: 04/18/2021 12:18:40\n", " Finished: 04/18/2021 12:18:45\n", " Total time elapsed: 00:00:05\n" ] } ], "source": [ "import random\n", "\n", "random.seed(1)\n", "collection = set()\n", "\n", "n = 100\n", "bar = pyprind.ProgBar(n, track_time=False, title='while example')\n", "\n", "while len(collection) < n:\n", " r = random.randint(0, 10**5)\n", " if r % 7 and r not in collection:\n", " collection.add(r)\n", " bar.update()\n", " time.sleep(timesleep)\n", "\n", "print(bar)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Percentage Indicator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*Note: the Percentage indicator is significantly slower due to background computation. \n", "Thus, it is recommended for tasks with less iterations but longer computational time per iteration.*" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "perc = pyprind.ProgPercent(n)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " perc.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar and Percentage Indicator generators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Alternatively, you can use the progress bar and percentage indicators as generators." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "for i in pyprind.prog_bar(range(n)):\n", " time.sleep(timesleep) # your computation here" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "for i in pyprind.prog_percent(range(n)):\n", " time.sleep(timesleep) # your computation here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar/Percentage Indicator - Reporting tracking information" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simply `print()` the tracking object after the tracking has completed." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Title: \n", " Started: 04/18/2021 12:19:02\n", " Finished: 04/18/2021 12:19:07\n", " Total time elapsed: 00:00:05\n" ] } ], "source": [ "bar = pyprind.ProgBar(n)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()\n", "print(bar)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Job_1\n", "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Title: Job_1\n", " Started: 04/18/2021 12:19:07\n", " Finished: 04/18/2021 12:19:13\n", " Total time elapsed: 00:00:05\n", " CPU %: 0.00\n", " Memory %: 0.32\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, monitor=True, title='Job_1')\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()\n", " \n", "# print report for future reference\n", "print(bar)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar/Percentage Indicator - Reporting CPU and memory usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`monitor` (`bool`): default False. Monitors CPU and memory usage if `True` \n", " (requires the [`psutil`](https://pypi.python.org/pypi/psutil) package)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Title: \n", " Started: 04/18/2021 12:19:13\n", " Finished: 04/18/2021 12:19:18\n", " Total time elapsed: 00:00:05\n", " CPU %: 0.00\n", " Memory %: 0.32\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, monitor=True)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()\n", "print(bar)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Title: \n", " Started: 04/18/2021 12:19:18\n", " Finished: 04/18/2021 12:19:24\n", " Total time elapsed: 00:00:05\n", " CPU %: 0.10\n", " Memory %: 0.32\n" ] } ], "source": [ "perc = pyprind.ProgPercent(n, monitor=True)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " perc.update()\n", "print(perc)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar/Percentage Indicator - Setting a title" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`title` (`str`): default ''. A title for the progress bar" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "My 1st Progress Bar\n", "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, title='My 1st Progress Bar')\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "My 1st Percent Tracker\n", "[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "perc = pyprind.ProgPercent(n, title='My 1st Percent Tracker')\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " perc.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar - Changing the default width" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`width` (`int`): default 30. Sets the progress bar width in characters." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##########] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, width=10)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [######################################################################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, width=70)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar/Percentage Indicator - Changing the output stream" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`stream` (`int`): default 2. Takes 1 for stdout, 2 for stderr, or given stream object" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, stream=1)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:05\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, stream=2)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "bar = pyprind.ProgBar(n, stream=sys.stdout)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " bar.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stopping the Progress Bar/Percentage Indicator early" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The tracking object can be stopped early via the `.stop()` method:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [##############################] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:00\n" ] } ], "source": [ "bar = pyprind.ProgBar(n)\n", "for i in range(n):\n", " time.sleep(timesleep) # your computation here\n", " if i == 5:\n", " bar.stop()\n", " break\n", " bar.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Progress Bar/Percentage Indicator - Printing currently processed items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes it is useful to print out the name of currently processed items, e.g., files are being processed. This can be done by providing a custom string for the optional `item_id` parameter of the `.update()` method." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [####################] 100% | ETA: 00:00:00 | Item ID: file_20.csv\n", "Total time elapsed: 00:00:01\n" ] } ], "source": [ "items = ['file_%s.csv' %i for i in range(1,21)]\n", "\n", "bar = pyprind.ProgBar(len(items))\n", "for i in items:\n", " time.sleep(timesleep) # your computation here\n", " bar.update(item_id = i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Choosing your own progress bar style" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [██████████████████████████████] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:10\n" ] } ], "source": [ "bar = pyprind.ProgBar(n, bar_char='█')\n", "for i in range(n):\n", " time.sleep(0.1) # do some computation\n", " bar.update()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Controlling the update frequency" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[[back to section overview](#sections)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Update the progress in after iteration of the loop via the `force_flush` parameter." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [██████████████████████████████] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:50\n" ] } ], "source": [ "n = 100\n", "bar = pyprind.ProgBar(n, bar_char='█')\n", "for i in range(n):\n", " time.sleep(0.5) # do some computation\n", " bar.update(force_flush=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Update the progress every 4 seconds using the `update_interval` parameter." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "0% [██████████████████████████████] 100% | ETA: 00:00:00\n", "Total time elapsed: 00:00:20\n" ] } ], "source": [ "n = 100\n", "bar = pyprind.ProgBar(n, bar_char='█', update_interval=4)\n", "for i in range(n):\n", " time.sleep(0.2) # do some computation\n", " bar.update()" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[100 %] Time elapsed: 00:00:20 | ETA: 00:00:00\n", "Total time elapsed: 00:00:20\n" ] } ], "source": [ "n = 100\n", "bar = pyprind.ProgPercent(n, update_interval=4)\n", "for i in range(n):\n", " time.sleep(0.2) # do some computation\n", " bar.update()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }