Enlighten Progress Bar is a console progress bar library for Python
—
Helper functions and classes for time formatting, warnings, text justification, and HTML conversion. These utilities support the core functionality and provide additional tools for working with enlighten components.
Format time durations in human-readable format for display in progress bars and status updates.
def format_time(seconds):
"""
Format time duration in human-readable format.
Parameters:
- seconds: Time duration in seconds (float)
Returns:
str: Formatted time string
Formats time in one of these forms based on duration:
- 01:56 (minutes:seconds)
- 12h 01:56 (hours, minutes:seconds)
- 1d 0h 01:56 (days, hours, minutes:seconds)
"""Enumerated constants for text alignment in progress bars and status displays.
class Justify:
"""
Text justification constants for alignment options.
"""
CENTER = 'center' # Center-align text
LEFT = 'ljust' # Left-align text (default)
RIGHT = 'rjust' # Right-align textCustom warning class for enlighten-specific warnings and issues.
class EnlightenWarning(Warning):
"""
Generic warning class for Enlighten library warnings.
Used for configuration issues, compatibility warnings,
and other non-fatal problems in enlighten operations.
"""ANSI terminal code to HTML converter for Jupyter notebook display.
class HTMLConverter:
def __init__(self, term):
"""
ANSI to HTML converter for notebook environments.
Parameters:
- term: Blessed Terminal instance
"""
def to_html(self, text):
"""
Convert ANSI-formatted text to HTML.
Parameters:
- text: String with ANSI escape codes
Returns:
str: HTML-formatted text with CSS classes
Supported formatting:
- Colors (8, 16, 256, RGB)
- Bold, italic, underline
- Blink animation
- Hyperlinks
"""
@property
def style(self):
"""
CSS style definitions for converted HTML.
Returns:
str: Complete CSS style block for HTML output
"""Additional utility classes and functions used internally by enlighten.
class Lookahead:
def __init__(self, iterator):
"""
Iterator wrapper supporting look-ahead operations.
Parameters:
- iterator: Source iterator to wrap
"""
def __getitem__(self, key):
"""
Access future items without consuming them.
Parameters:
- key: Index or slice for look-ahead access
Returns:
Item(s) from future positions in iterator
"""
def warn_best_level(message, category):
"""
Issue warning at appropriate stack level outside library.
Parameters:
- message: Warning message text
- category: Warning category class
"""import enlighten
# Format various time durations
print(enlighten.format_time(65)) # "01:05"
print(enlighten.format_time(3665)) # "1h 01:05"
print(enlighten.format_time(90065)) # "1d 1h 01:05"
# Use in custom formatting
elapsed = 3725.5
formatted_time = enlighten.format_time(elapsed)
print(f"Operation completed in {formatted_time}")
# Output: "Operation completed in 1h 02:05"import enlighten
# Create progress bar with center-aligned description
pbar = enlighten.Counter(
total=100,
desc='Centered Progress',
justify=enlighten.Justify.CENTER
)
# Create status bar with right-aligned text
status = enlighten.StatusBar(
justify=enlighten.Justify.RIGHT
)
# Available justification options
print(enlighten.Justify.LEFT) # 'ljust'
print(enlighten.Justify.CENTER) # 'center'
print(enlighten.Justify.RIGHT) # 'rjust'import enlighten
import warnings
# Configure warning handling
warnings.filterwarnings('always', category=enlighten.EnlightenWarning)
# Enlighten will issue warnings for configuration issues
# For example, if trying to use features not supported in current environment
try:
# Some operation that might trigger a warning
manager = enlighten.Manager(some_unsupported_option=True)
except Exception as e:
print(f"Configuration issue: {e}")# Internal use in NotebookManager - normally not called directly
from blessed import Terminal
import enlighten._util
# Create HTML converter
term = Terminal()
converter = enlighten._util.HTMLConverter(term)
# Convert ANSI text to HTML
ansi_text = "\x1b[32mGreen text\x1b[0m with \x1b[1mbold\x1b[0m formatting"
html_output = converter.to_html(ansi_text)
css_styles = converter.style
# HTML output ready for notebook display
print(html_output)
print(css_styles)import enlighten
import time
# Using format_time in custom status formats
status = enlighten.StatusBar(
status_format='Uptime: {uptime} | Status: {status}'
)
start_time = time.time()
for i in range(10):
current_uptime = time.time() - start_time
status.update(
uptime=enlighten.format_time(current_uptime),
status=f'Running task {i+1}'
)
time.sleep(1)
status.close()# Internal utility - normally not used directly
import enlighten._util
# Wrap iterator with look-ahead capability
data = range(10)
lookahead = enlighten._util.Lookahead(iter(data))
# Look ahead without consuming
print(lookahead[0]) # 0 (peek at next item)
print(lookahead[1:3]) # [1, 2] (peek at slice)
# Normal iteration still works
print(next(lookahead)) # 0 (consume first item)
print(next(lookahead)) # 1 (consume second item)import enlighten
import warnings
# Capture enlighten warnings in your application
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always", enlighten.EnlightenWarning)
# Operations that might generate warnings
manager = enlighten.get_manager()
pbar = manager.counter(total=100)
# Check for any warnings
if w:
for warning in w:
if issubclass(warning.category, enlighten.EnlightenWarning):
print(f"Enlighten warning: {warning.message}")Install with Tessl CLI
npx tessl i tessl/pypi-enlighten