Friday, 16 August 2024

Enhancing Python Scripts with Alive-Progress: 5 Practical Examples

 pip install alive-progress


1. Simple Progress Bar

from alive_progress import alive_bar

import time

items = range(100)

with alive_bar(len(items)) as bar:

    for item in items:

        time.sleep(0.05)  

        bar()

|████████████████████████████████████████| 100/100 [100%] in 5.0s (19.83/s) 

2. Progress Bar with Custom Text

from alive_progress import alive_bar

import time


tasks = ['task1', 'task2', 'task3', 'task4']


with alive_bar(len(tasks), title='Processing Tasks') as bar:

    for task in tasks:

        time.sleep(0.5)  

        bar.text = f'Working on {task}'

        bar()

Processing Tasks |████████████████████████████████████████| 4/4 [100%] in 2.0s (2.00/s) 


3. Nested Progress Bars

from alive_progress import alive_bar

import time

outer_items = range(3)

inner_items = range(5)


with alive_bar(len(outer_items) * len(inner_items), title='Processing') as bar:

    for _ in outer_items:

        for _ in inner_items:

            time.sleep(0.1)  # Simulate work

            bar()

Processing |████████████████████████████████████████| 15/15 [100%] in 1.5s (9.94/s) 



4. Download Simulation with Progress Bar

from alive_progress import alive_bar

import time

file_size = 1024  

chunk_size = 64

with alive_bar(file_size // chunk_size, title='Downloading') as bar:

    for _ in range(0, file_size, chunk_size):

        time.sleep(0.1) 

        bar()

Downloading |████████████████████████████████████████| 16/16 [100%] in 1.6s (9.83/s) 

5. Progress Bar with Percentage and ETA

from alive_progress import alive_bar

import time

total_items = 50

with alive_bar(total_items, title='Processing', 

               spinner='dots_waves', bar='smooth') as bar:

    for _ in range(total_items):

        time.sleep(0.1)  

        bar()

Processing |████████████████████████████████████████| 50/50 [100%] in 5.0s (9.95/s) 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (3) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (205) Cybersecurity (24) data management (11) Data Science (109) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (27) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (50) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (898) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses