Skip to main content

A simple progress loader for Python

Project description

███████▒▒▒ 70%

Throttle: A Python Progress Indicator Library

Tired of watching your code run without any feedback? ⏳ The throttle library makes it easy to add progress bars, spinners, clocks, and dots to your Python scripts, so you can see the progress of long-running operations.

Features

  • Multiple Styles:
    • Bar Style: Classic progress bar with percentage and completed/total items.
    • Spinner Style: A simple spinning animation for visual feedback.
    • Dots Style: A minimalist style with dots that increment as progress is made.
    • Clock Style: A clock-style progress bar where the hands move as progress is made.
  • Customization:
    • Color: Choose from blue, green, or red (or leave blank for the default).
    • Description: Add a custom description to the progress indicator.
    • Fill and Empty Characters: Customize the appearance of the bar with different characters.
    • Refresh Rate: Control how often the progress indicator updates.
  • Flexibility:
    • Manual Updates: Update the progress indicator directly with the update() method.
    • Decorator: Easily apply a progress indicator to any function with the progress_decorator decorator.
    • Callback Functions: Customize the rendering of the progress indicator with custom callback functions.

Getting Started

To get started with the throttle library, simply install it via pip:

pip install throttle
# Import the necessary components
from throttle import Throttle, progress_decorator

# Create a progress bar
throttle = Throttle(total=10, desc="My Progress", style="bar", color="blue")

# Start the progress bar
throttle.start()

# Update the progress
throttle.update() # Increment the progress by 1
throttle.update(amount=3) # Increment the progress by 3

# Stop the progress bar
throttle.close()

Usage

from throttle import Throttle, my_example_function, process_data

test_data = list(range(1, 11))

# Bar style progress bar (blue)
with Throttle(total=10, desc="Processing data", style="bar", color="blue") as test_throttle:
  test_throttle.with_function(my_example_function, test_data)

# Spinner style progress bar
with Throttle(total=10, desc="Processing data", spinner=True) as test_throttle:
  test_throttle.with_function(my_example_function, test_data)

# Dots style progress bar (custom fill/empty characters)
with Throttle(total=10, desc="Processing data", style="dots", fill_char="*", empty_char=".") as test_throttle:
  test_throttle.with_function(my_example_function, test_data)

# Clock style progress bar 
with Throttle(total=10, desc="Processing data", style="clock") as test_throttle:
  test_throttle.with_function(my_example_function, test_data)

# Manual updates (bar style)
test_throttle = Throttle(total=5, desc="Loading", style="bar", color="green")
test_throttle.start()
for i in range(5):
  test_throttle.update()
  time.sleep(0.5)
test_throttle.close()

Using the progress_decorator

from throttle import progress_decorator, process_data

test_data = list(range(1, 11))

@throttle_decorator(total=10, desc="Processing data", style="bar", color="blue")
def process_data(data: List[int], throttle: Throttle):
  for item in data:
    my_example_function(item, throttle)

process_data(test_data)

Custom Callback Functions

from throttle import Throttle, my_example_function, process_data

test_data = list(range(1, 11))

def custom_callback(throttle):
  print(f"Progress: {throttle.progress}/{throttle.total}")
  print(f"Percentage: {throttle.percentage:.2f}%")
  print(f"Elapsed Time: {throttle.elapsed_time:.2f}s")
  print(f"Estimated Time Remaining: {throttle.eta:.2f}s")

  # Custom logic here

with Throttle(total=10, desc="Processing data", style="bar", color="blue",
              callback=custom_callback) as test_throttle:
  test_throttle.with_function(my_example_function, test_data)

Customizing the Progress Bar Appearance

from throttle import Throttle

with Throttle(total=10, desc='Custom Progress Bar', style='bar', color='red', fill_char='*', empty_char='.') as throttle:
  for i in range(10):
    throttle.update()
    time.sleep(0.5)

print("Progress bar completed")

Real-World Examples 🌎

1. Downloading Files

from throttle import Throttle
import requests

def download_file(url, filename, throttle):
  with requests.get(url, stream=True) as response:
    response.raise_for_status()
    total_size = int(response.headers.get('content-length', 0))
    with open(filename, 'wb') as file:
      for chunk in response.iter_content(chunk_size=8192):
        if chunk:
          file.write(chunk)
          throttle.update(len(chunk))

url = "https://example.com/large_file.zip"
filename = "large_file.zip"

with Throttle(total=total_size, unit='bytes', desc='Downloading File') as throttle:
  download_file(url, filename, throttle)

print(f"File downloaded: {filename}")

2. Processing a Large Dataset

from throttle import progress_decorator, Throttle
import pandas as pd

@throttle_decorator(total=len(data), desc='Processing Data')
def process_data(data: pd.DataFrame, throttle: Throttle):
  for index, row in data.iterrows():
    # Process each row of the DataFrame
    # ...
    throttle.update()

data = pd.read_csv("large_dataset.csv")
process_data(data)

3. Running Multiple Tasks Concurrently

from throttle import Throttle
from threading import Thread

def long_running_task(task_id, throttle):
  for i in range(10):
    # Simulate some work
    time.sleep(0.5)
    throttle.update()
  print(f"Task {task_id} completed")

with Throttle(total=30, desc='Running Tasks') as throttle:
  threads = [Thread(target=long_running_task, args=(i, throttle)) for i in range(3)]
  for thread in threads:
    thread.start()
  for thread in threads:
    thread.join()

print("All tasks completed")

4. Custom Callback Function Long Running Task

from throttle import Throttle
import time

def long_running_task(total, throttle):
  for i in range(total):
    # Simulate some work
    time.sleep(0.5)
    throttle.update()

def custom_callback(throttle):
  print(f"Progress: {throttle.progress}/{throttle.total}")
  print(f"Percentage: {throttle.percentage:.2f}%")
  print(f"Elapsed Time: {throttle.elapsed_time:.2f}s")
  print(f"Estimated Time Remaining: {throttle.eta:.2f}s")
  print("-" * 20)

with Throttle(total=10, desc='Running Task', callback=custom_callback) as throttle:
  long_running_task(10, throttle)

print("Task completed")

6. Generating Thumbnails

from throttle import Throttle
import requests
from PIL import Image
from io import BytesIO
import os
import logging

def generate_thumbnails(image_urls, thumbnail_dir, width, height):
  success_count = [0]
  error_count = [0]

  with Throttle(total=len(image_urls), desc="Generating Thumbnails", style="time_clock",
                color="blue") as throttle:
    for image_url in image_urls:
      try:
        response = requests.get(image_url)
        response.raise_for_status()
        image = Image.open(BytesIO(response.content))
        thumbnail_path = os.path.join(thumbnail_dir, os.path.basename(image_url))
        image.thumbnail((width, height))
        image.save(thumbnail_path)
        logging.info(f"Generated thumbnail: {thumbnail_path}")
        success_count[0] += 1
      except Exception as e:
        logging.error(f"Error generating thumbnail for {image_url}: {e}")
        error_count[0] += 1
      finally:
        throttle.update()

  logging.info(f"Thumbnail generation completed with {success_count[0]} successes and {error_count[0]} errors.")

MANIFEST.in

include README.md

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

throttle_loader-0.1.0.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

throttle_loader-0.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file throttle_loader-0.1.0.tar.gz.

File metadata

  • Download URL: throttle_loader-0.1.0.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for throttle_loader-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9fa67c62dd91136183e4fa5e22eb4aa1ac3c9b62805ca30403f1c9c223d51c35
MD5 571a6ceb8222c1ee656710fd69722447
BLAKE2b-256 707949f47b66cea29c433d5fa5008492c3a7c3be13fa79f58065fa467640d9d4

See more details on using hashes here.

File details

Details for the file throttle_loader-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for throttle_loader-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b91a2c26545ee4fe034f1110bd5e3cbc1d8bf20232f4644a8a2a8875e9efed16
MD5 fc8fc8b43111d5d0397cf0e1f498a9b4
BLAKE2b-256 42c1a02f37587195a8e1144fac8b9a87fe7a5cadcb0786cb5e336fc852c6ab53

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page