Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Display a Progress Bar in Python

#1
How to Display a Progress Bar in Python

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;508394&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>[<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />] 100%</td>
</tr>
</tbody>
</table>
</figure>
<h2 class="wp-embed-aspect-4-3 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p class="wp-embed-aspect-4-3 wp-has-aspect-ratio">In this article, you’ll learn how to configure and display a progress bar.</p>
<p>A progress bar is commonly used in Python or, for that matter, any other programming language to show the user an application’s progress. For example, an installation, a transferring of files, or any other commands. </p>
<p>The <a rel="noreferrer noopener" href="https://academy.finxter.com/" data-type="URL" data-id="https://academy.finxter.com/" target="_blank">Finxter Academy</a> recommends implementing this to visually display to the user what is happening behind the scenes. </p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to display a progress bar?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method 1</strong>: Use a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop</li>
<li><strong>Method 2</strong>: Use <code><a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" data-type="URL" data-id="https://pypi.org/project/apytl/" target="_blank"></a><a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" data-type="URL" data-id="https://pypi.org/project/apytl/" target="_blank">apytl</a> </code>library <a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" data-type="URL" data-id="https://pypi.org/project/apytl/" target="_blank"><code>Bar().drawbar()</code></a> function</li>
<li><strong>Method 3</strong>: Use <a rel="noreferrer noopener" href="https://pypi.org/project/alive-progress/1.0/" data-type="URL" data-id="https://pypi.org/project/alive-progress/1.0/" target="_blank"><code>alive-progress</code></a></li>
<li><strong>Method 4</strong>: Use <a rel="noreferrer noopener" href="https://pypi.org/project/tqdm/" data-type="URL" data-id="https://pypi.org/project/tqdm/" target="_blank"><code>tqdm</code></a></li>
<li><strong>Method 5</strong>: Use <a href="https://pypi.org/project/progress/" data-type="URL" data-id="https://pypi.org/project/progress/" target="_blank" rel="noreferrer noopener"><code>progress</code></a></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 1: Use a For Loop</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="587" height="486" src="https://blog.finxter.com/wp-content/uploads/2022/07/progress_bar.gif" alt="" class="wp-image-514363" /></figure>
</div>
<p class="has-global-color-8-background-color has-background">This method imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-sys/" data-type="URL" data-id="https://blog.finxter.com/category/python-sys/" target="_blank"><code>sys</code></a> libraries combined with a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop to display a custom progress bar output on a <a href="https://blog.finxter.com/python-one-line-x/" data-type="post" data-id="10612" target="_blank" rel="noreferrer noopener">single line</a>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from time import sleep
import sys for x in range(0,21): sys.stdout.write('\r') sys.stdout.write("[%-20s] %d%%" % ('<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />'*x, 5*x)) sys.stdout.flush() sleep(0.75)</pre>
<p>Above imports, the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> library to call the sleep command and the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-sys/" data-type="URL" data-id="https://blog.finxter.com/category/python-sys/" target="_blank">sys</a> </code>library to write the contents to the terminal.</p>
<p>Next, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop is instantiated. This loop uses the <a href="https://blog.finxter.com/python-range-function/" data-type="URL" data-id="https://blog.finxter.com/python-range-function/"><code>range</code></a> function to set the start position (0 by default) and the stop position (21-1). Inside this loop, the following occurs:</p>
<ul>
<li>The first line uses a carriage return (<code>\r</code>) to start the code on a new line.</li>
<li>The following line determines how the progress bar appears by:
<ul>
<li>Left-aligning the emoji(s) inside the square brackets <code>[%-20s]</code>.</li>
<li>Configuring the progress percentage <code>%d%%" %</code> (example <code>15%</code>).</li>
<li>Determining the visual emoji progress <code>('&#x1f601;'*x, 5*x)</code>.</li>
</ul>
</li>
<li>Then <code>sys.stdout.flush()</code> writes everything in the buffer to the terminal.</li>
<li>The code pauses for the stated period (<code>0.75</code>).</li>
</ul>
<p>The loop continues until 100% has been attained. Below is the end result.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>[<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f601.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />] 100%</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note: </strong>Any emoji or another symbol can be substituted for the above selection. </p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 2: Use apytl Library</h2>
<p class="has-global-color-8-background-color has-background">This method imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> and <a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" data-type="URL" data-id="https://pypi.org/project/apytl/" target="_blank"><code>apytl</code></a> libraries to generate a custom progress bar. </p>
<p>To run this code error-free, install the required library. Click <a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" data-type="URL" data-id="https://pypi.org/project/apytl/" target="_blank">here</a> for installation instructions.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="7" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time
import apytl total_iterations = 10 for index, value in enumerate(range(total_iterations)): apytl.Bar().drawbar(value, total_iterations, fill='*') time.sleep(0.75)</pre>
<p>Above imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> library to call the sleep command and the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-sys/" data-type="URL" data-id="https://blog.finxter.com/category/python-sys/" target="_blank"></a><a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" target="_blank">apytl</a></code> library to display the progress bar.</p>
<p>Next, the total number of iterations to carry out is declared as <code>10</code> and saved to <code>total_iterations</code>.</p>
<p>To understand what is going on in the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop, let’s write some test code to see what the values of <code>index</code>, <code>value</code> and <code>total_iterations</code> are doing:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">total_iterations = 10 for index, value in enumerate(range(total_iterations)): print(index, value, total_iterations)</pre>
<p>As you can see from the output, the <code>index</code> and <code>value </code>variables count from 0 (<a href="https://blog.finxter.com/python-range-function/" data-type="URL" data-id="https://blog.finxter.com/python-range-function/"><code>range</code></a> default start position) to the stop position of 9 (10-1). Notice the value of <code>total_iterations</code> does not change.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>0 0 10<br />1 1 10<br />2 2 10<br />3 3 10<br />4 4 10<br />5 5 10<br />6 6 10<br />7 7 10<br />8 8 10<br />9 9 10</code></td>
</tr>
</tbody>
</table>
</figure>
<p>Referring back to the code directly below the Method 2 heading, the next line is: <code>apytl.Bar().drawbar(value, total_iterations, fill='*')</code>.</p>
<p>This function is passed three (3) arguments:</p>
<ul>
<li>The variable <code>value</code>, which you can see from the above, increments by one (1) each iteration.</li>
<li>The variable <code>total_iterations</code>, which remains constant at <code>10</code>.</li>
<li>The fill value. This example uses the asterisk (<code>*</code>) character. However, feel free to use an emoji or other symbol.</li>
</ul>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Progress |***********************************| 100.0% Complete</td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 3: Use alive-progress</h2>
<p class="has-global-color-8-background-color has-background">The <a rel="noreferrer noopener" href="https://pypi.org/project/alive-progress/1.0/" data-type="URL" data-id="https://pypi.org/project/alive-progress/1.0/" target="_blank"><code>alive-progress</code></a> library moves progress bars to the next level! With its many display options, it’s a must-a-try!</p>
<p>To run this code error-free, install the required library. Click <a href="https://pypi.org/project/alive-progress/1.0/" data-type="URL" data-id="https://pypi.org/project/alive-progress/1.0/" target="_blank" rel="noreferrer noopener">here</a> for installation instructions.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from alive_progress import alive_bar
from time import sleep for x in range(10): with alive_bar(x, bar='solid') as bar: for i in range(x): sleep(.001) bar()</pre>
<p>Above imports the <a rel="noreferrer noopener" href="https://pypi.org/project/alive-progress/1.0/" data-type="URL" data-id="https://pypi.org/project/alive-progress/1.0/" target="_blank"><code>alive-progress</code></a> library to use the progress bar and the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> library to call the <code>sleep</code> command.</p>
<p>Next, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop is instantiated. This loop uses the <a href="https://blog.finxter.com/python-range-function/" data-type="URL" data-id="https://blog.finxter.com/python-range-function/"><code>range</code></a> function to set the stop position at 9 (10-1). Inside this loop, the following occurs:</p>
<ul>
<li>The <code>alive_bar </code>is called and passed the argument <code>x</code> and the bar type. For this example, <code>solid </code>was selected.
<ul>
<li>A new <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop is instantiated and loops through <a href="https://blog.finxter.com/python-range-function/" data-type="URL" data-id="https://blog.finxter.com/python-range-function/"><code>range</code></a> passing <code>x</code> as an argument.</li>
<li>The code halts (<code>sleep</code>) for the stated period (<code>.001</code>).</li>
<li>The bar is output to the terminal each iteration.</li>
</ul>
</li>
</ul>
<p>The loop continues until 100% has been attained.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td> &lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 0 in 0.0s (0.00/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 1/1 [100%] in 0.0s (333.28/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 2/2 [100%] in 0.0s (62.48/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 3/3 [100%] in 0.0s (63.96/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 4/4 [100%] in 0.1s (62.39/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 5/5 [100%] in 0.1s (63.32/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 6/6 [100%] in 0.1s (64.84/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 7/7 [100%] in 0.1s (63.79/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 8/8 [100%] in 0.1s (63.53/s)<br />&lt;■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■&gt; 9/9 [100%] in 0.1s (64.09/s)</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note: </strong>To view a list of all available progress bars, click <a rel="noreferrer noopener" href="https://pypi.org/project/alive-progress/1.0/" data-type="URL" data-id="https://pypi.org/project/alive-progress/1.0/" target="_blank">here</a>.</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 4: Use tqdm</h2>
<p class="has-global-color-8-background-color has-background">A simple yet elegant progress bar, <a rel="noreferrer noopener" href="https://pypi.org/project/tqdm/" data-type="URL" data-id="https://pypi.org/project/tqdm/" target="_blank"><code>tqdm</code></a> is a must-see!</p>
<p>To run this code error-free, install the required library. Click<a rel="noreferrer noopener" href="https://pypi.org/project/alive-progress/1.0/" data-type="URL" data-id="https://pypi.org/project/alive-progress/1.0/" target="_blank"> </a><a href="https://pypi.org/project/tqdm/" data-type="URL" data-id="https://pypi.org/project/tqdm/" target="_blank" rel="noreferrer noopener">here</a> for installation instructions.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from tqdm import tqdm
from time import sleep for i in tqdm(range(100)): sleep(0.02)</pre>
<p>Above imports the <a rel="noreferrer noopener" href="https://pypi.org/project/tqdm/" data-type="URL" data-id="https://pypi.org/project/tqdm/" target="_blank"><code>tqdm</code></a> library for the progress bar and the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> library to call the <code><a href="https://blog.finxter.com/time-delay-in-python/" data-type="post" data-id="138154">sleep()</a></code> command.</p>
<p>Next, a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop is instantiated. This loop uses the <a rel="noreferrer noopener" href="https://pypi.org/project/tqdm/" data-type="URL" data-id="https://pypi.org/project/tqdm/" target="_blank"><code>tqdm</code></a> library with the <a href="https://blog.finxter.com/python-range-function/" data-type="URL" data-id="https://blog.finxter.com/python-range-function/"><code>range</code></a> function to set the stop position at 99 (100-1). Inside this loop, the following occurs: </p>
<p>The progress bar displays on one line. Each iteration increases the progress by a percentage.</p>
<figure class="wp-block-image size-full"><img loading="lazy" width="852" height="26" src="https://blog.finxter.com/wp-content/uploads/2022/07/bbbb.png" alt="" class="wp-image-513334" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/bbbb.png 852w, https://blog.finxter.com/wp-content/uplo...-300x9.png 300w, https://blog.finxter.com/wp-content/uplo...768x23.png 768w" sizes="(max-width: 852px) 100vw, 852px" /></figure>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Method 5: Use progress Library</h2>
<p class="has-global-color-8-background-color has-background">This method uses the <a rel="noreferrer noopener" href="https://pypi.org/project/progress/" data-type="URL" data-id="https://pypi.org/project/progress/" target="_blank"><code>progress</code></a> library to display a progress bar. This library has many fun options to select from.</p>
<p>To run this code error-free, install the required library. Click<a rel="noreferrer noopener" href="https://pypi.org/project/tqdm/" data-type="URL" data-id="https://pypi.org/project/tqdm/" target="_blank"> </a><a href="https://pypi.org/project/progress/" data-type="URL" data-id="https://pypi.org/project/progress/" target="_blank" rel="noreferrer noopener">here</a> for installation instructions.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from time import sleep
from progress.spinner import MoonSpinner with MoonSpinner('Processing…') as bar: for i in range(100): sleep(0.02) bar.next()</pre>
<p>Above imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/category/python-time/" data-type="URL" data-id="https://blog.finxter.com/category/python-time/" target="_blank"><code>time</code></a> library to call the sleep command and the<a rel="noreferrer noopener" href="https://pypi.org/project/apytl/" target="_blank"> </a><a rel="noreferrer noopener" href="https://pypi.org/project/progress/" data-type="URL" data-id="https://pypi.org/project/progress/" target="_blank"><code>progress</code></a> library to display a progress bar, specifically the <code>MoonSpinner</code>.</p>
<p>The following code displays the word <code>Processing...</code> at the terminal, and the <code>MoonSpinner</code> rotates, sleeps, and continues until the value of the <a href="https://blog.finxter.com/python-range-function/" data-type="URL" data-id="https://blog.finxter.com/python-range-function/"><code>range</code></a> stop position (100-1) is attained.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td>Processing…◑</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note: </strong>Spend some time on this library <a href="https://pypi.org/project/progress/" data-type="URL" data-id="https://pypi.org/project/progress/" target="_blank" rel="noreferrer noopener">page</a> to test the different types of progress bars.</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Summary</h2>
<p>There is so much more to Python Progress Bars than was covered in this article. May we suggest you take time to delve into each method outlined above to determine the best one for your requirements.</p>
<p>Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<h2>Programming Humor – Python</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="518" height="588" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-65.png" alt="" class="wp-image-471102" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-65.png 518w, https://blog.finxter.com/wp-content/uplo...64x300.png 264w" sizes="(max-width: 518px) 100vw, 518px" /><figcaption><em>“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”</em> — <a rel="noreferrer noopener" href="https://imgs.xkcd.com/comics/python.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/python.png" target="_blank">xkcd</a></figcaption></figure>
</div></p>
</div>


https://www.sickgaming.net/blog/2022/07/...in-python/
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016