Python tqdm Module | How to create a terminal progress barWhenever we install any Python library, module, or installing software, we see the progress bar to screen, which denotes the small progress bar that estimates how long the process would take to complete or render. It gives us an impression of activity and can calm the nerves. We all are familiar with the various progress bar. Some of them are attractive and some of them boring. In this article, we will create the progress bar without involving Python's code core logging library. Progress bars are filled up according to the percentage of progress made in accomplishing a task. The progress can be calculated by dividing number_of_item_processed by total_input_item. Various factors affect the progress bar, such as network speed, latency, and if persisting data into local storage to derive a more accurate ETA (Estimated Time of Arrival). We can create simple and hassle-free progress bars using the Python external library named tqdm. We can add it to the code and make it look lovely. The tqdm stands for taqadum in Arabic, which means progress. Python tqdm module works on various platform such Linux, Window, Mac, etc. and it is also compatible with the IPython/Jupyter notebooks. Need for Progress BarIf we are working with the smaller data set, the progress will not bother in our workflow. However, the progress bar can be used for iterating over a dataset, training a model, or encoding a large information set.
PrerequisitesPython 3 must be installed in the system and we can also create a virtual environment to install the tqdm library. InstallationOpen the command-line terminal and type the following. Or The above command will successfully install in the system. We can verify it using the following statement. If there is no error which means this library is successfully installed. Adding Progress Bars to for LoopsProgram Output: outer loop: 0%| | 0/10 [00:00, ?it/s] inner loop: 0%| | 0.00/100 [00:00, ?it/s] inner loop: 1%| | 1.00/100 [00:01<01:40, 1.02s/it] inner loop: 2%|▏ | 2.00/100 [00:02<01:39, 1.02s/it] inner loop: 3%|▎ | 3.00/100 [00:03<01:38, 1.01s/it] inner loop: 4%|▍ | 4.00/100 [00:04<01:37, 1.01s/it] inner loop: 5%|▌ | 5.00/100 [00:05<01:35, 1.01s/it] inner loop: 6%|▌ | 6.00/100 [00:06<01:34, 1.01s/it] inner loop: 7%|▋ | 7.00/100 [00:07<01:34, 1.01s/it] inner loop: 8%|▊ | 8.00/100 [00:08<01:33, 1.02s/it] inner loop: 9%|▉ | 9.00/100 [00:09<01:32, 1.02s/it] inner loop: 10%|█ | 10.0/100 [00:10<01:31, 1.02s/it] inner loop: 11%|█ | 11.0/100 [00:11<01:30, 1.01s/it] inner loop: 12%|█▏ | 12.0/100 [00:12<01:29, 1.01s/it] inner loop: 13%|█▎ | 13.0/100 [00:13<01:28, 1.01s/it] inner loop: 14%|█▍ | 14.0/100 [00:14<01:29, 1.04s/it] inner loop: 15%|█▌ | 15.0/100 [00:15<01:27, 1.03s/it] inner loop: 16%|█▌ | 16.0/100 [00:16<01:26, 1.02s/it] inner loop: 17%|█▋ | 17.0/100 [00:17<01:24, 1.02s/it] inner loop: 18%|█▊ | 18.0/100 [00:18<01:23, 1.02s/it] inner loop: 19%|█▉ | 19.0/100 [00:19<01:22, 1.02s/it] ……………………………………………………………………………………………. …………………………………………………………………………………………….. It will go till it reached at 100%. Let's understand another example. Example - 1 Output:
Example - Output: outer loop: 0%| | 0/10 [00:00, ?it/s] inner loop: 0%| | 0.00/100 [00:00, ?it/s] inner loop: 1%| | 1.00/100 [00:01<01:39, 1.00s/it] inner loop: 2%|▏ | 2.00/100 [00:02<01:38, 1.00s/it] inner loop: 3%|▎ | 3.00/100 [00:03<01:37, 1.00s/it] inner loop: 4%|▍ | 4.00/100 [00:04<01:36, 1.00s/it] inner loop: 5%|▌ | 5.00/100 [00:05<01:35, 1.00s/it] inner loop: 6%|▌ | 6.00/100 [00:06<01:34, 1.01s/it] inner loop: 7%|▋ | 7.00/100 [00:07<01:33, 1.01s/it] inner loop: 8%|▊ | 8.00/100 [00:08<01:32, 1.01s/it] inner loop: 9%|▉ | 9.00/100 [00:09<01:31, 1.00s/it] inner loop: 10%|█ | 10.0/100 [00:10<01:30, 1.01s/it] Example -3 Output: Outer loop: 0%| | 0/3 [00:00, ?it/s] Inner loop: 0%| | 0/5 [00:00, ?it/s] Inner loop: 20%|██ | 1/5 [00:00<00:00, 4.62it/s] Inner loop: 40%|████ | 2/5 [00:00<00:00, 4.64it/s] Inner loop: 60%|██████ | 3/5 [00:00<00:00, 4.63it/s] Inner loop: 80%|████████ | 4/5 [00:00<00:00, 4.65it/s] Outer loop: 33%|███▎ | 1/3 [00:01<00:03, 1.58s/it] Inner loop: 0%| | 0/5 [00:00, ?it/s] Inner loop: 20%|██ | 1/5 [00:00<00:00, 4.91it/s] Inner loop: 40%|████ | 2/5 [00:00<00:00, 4.85it/s] Inner loop: 60%|██████ | 3/5 [00:00<00:00, 4.79it/s] Inner loop: 80%|████████ | 4/5 [00:00<00:00, 4.77it/s] Outer loop: 67%|██████▋ | 2/3 [00:03<00:01, 1.58s/it] Inner loop: 0%| | 0/5 [00:00, ?it/s] Inner loop: 20%|██ | 1/5 [00:00<00:00, 4.43it/s] Inner loop: 40%|████ | 2/5 [00:00<00:00, 4.56it/s] Inner loop: 60%|██████ | 3/5 [00:00<00:00, 4.65it/s] Inner loop: 80%|████████ | 4/5 [00:00<00:00, 4.72it/s] Outer loop: 100%|██████████| 3/3 [00:04<00:00, 1.57s/it] Predictive Manual Updates of Progress BarThe tqdm module provides a facility to update the progress bar at certain intervals manually. When we download a multi-part file in chunks or streaming data, we can manually update the process bar function. Let's understand the following example. Example - Output: 100%|██████████| 100/100 [00:10<00:00, 9.93it/s] Explanation - In the above code, we have set attribute as 100. The called function incremented by ten in each iteration until 100% is achieved. We can pass any value to the update() method. Threaded Progress BarWe can also trap the Python tqdm package into Python threads. Multiprocessing is the best way to use the total number of cores. The tqdm position argument allows us to specify the line offset to print this bar. It is set on automatic by default in case of unscripted. Let's understand the following example. The value must be specified to manage the multiple bars at once. If we ignore this argument, our bar will be overridden by different threads. Example - Output: progressbar #5: 0%| | 0/100 [00:00, ?it/s] progressbar #2: 0%| | 0/100 [00:00, ?it/s] progressbar #1: 0%| | 0/100 [00:00, ?it/s] progressbar #4: 0%| | 0/100 [00:00, ?it/s] progressbar #3: 0%| | 0/100 [00:00, ?it/s] progressbar #3: 10%|█ | 10/100 [00:01<00:09, 9.91it/s] progressbar #5: 10%|█ | 10/100 [00:01<00:09, 9.91it/s] progressbar #1: 10%|█ | 10/100 [00:02<00:18, 4.97it/s] progressbar #2: 10%|█ | 10/100 [00:02<00:18, 4.96it/s] progressbar #4: 10%|█ | 10/100 [00:02<00:18, 4.96it/s] progressbar #5: 15%|█▌ | 15/100 [00:02<00:11, 7.64it/s] progressbar #3: 15%|█▌ | 15/100 [00:02<00:11, 7.64it/s] progressbar #4: 15%|█▌ | 15/100 [00:03<00:17, 4.96it/s] progressbar #2: 15%|█▌ | 15/100 [00:03<00:17, 4.95it/s] progressbar #3: 20%|██ | 20/100 [00:04<00:16, 4.71it/s] progressbar #5: 25%|██▌ | 25/100 [00:04<00:11, 6.58it/s] progressbar #1: 20%|██ | 20/100 [00:04<00:16, 4.97it/s] progressbar #2: 20%|██ | 20/100 [00:04<00:16, 4.96it/s] Add Color in Tqdm Progress BarColor can make the progress bar very attractive. However, it doesn't add any new functionality to the working of the bar. The tqdm can work with Colorama, a simple cross-platform color terminal text in Python. Let's understand the following example. Example - Output: ![]() ConclusionWe have discussed all basic concepts related to the progress bar. Python comes with the tqdm module that helps us to design manually. We have defined suitable examples for important operation that we can perform in tqdm module. The tqdm module can collaborate with the subprocess and threads where we can run the multiple process bars at the same time. Next TopicCaesar Cipher in Python
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com