Skip to content

Instantly share code, notes, and snippets.

@Shivanshu-Gupta
Last active August 11, 2025 01:32
Show Gist options
  • Select an option

  • Save Shivanshu-Gupta/8c0b7f2fe7cc91f26e0f0ccec4e011a2 to your computer and use it in GitHub Desktop.

Select an option

Save Shivanshu-Gupta/8c0b7f2fe7cc91f26e0f0ccec4e011a2 to your computer and use it in GitHub Desktop.
rich-progress-bar
# Usage:
# for _ in track(range(10), "Doing stuff..."):
# pass
from typing import Callable, Iterable, List, Optional, Sequence, Union
from rich.progress import ProgressType, Progress, TextColumn, ProgressColumn, BarColumn, TimeElapsedColumn, TimeRemainingColumn
from rich.style import StyleType
from rich.console import Console
def get_progress_bar(
description="Working...",
auto_refresh=True,
console: Optional[Console] = None,
transient: bool = False,
get_time: Callable[[], float] = None,
refresh_per_second: float = 10,
style: StyleType = "bar.back",
complete_style: StyleType = "bar.complete",
finished_style: StyleType = "bar.finished",
pulse_style: StyleType = "bar.pulse",
disable: bool = False,
) -> Progress:
"""Track progress by iterating over a sequence.
Args:
sequence (Iterable[ProgressType]): A sequence (must support "len") you wish to iterate over.
description (str, optional): Description of task show next to progress bar. Defaults to "Working".
auto_refresh (bool, optional): Automatic refresh, disable to force a refresh after each iteration. Default is True.
transient: (bool, optional): Clear the progress on exit. Defaults to False.
console (Console, optional): Console to write to. Default creates internal Console instance.
refresh_per_second (float): Number of times per second to refresh the progress information. Defaults to 10.
style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.done".
pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
disable (bool, optional): Disable display of progress.
Returns:
Iterable[ProgressType]: An iterable of the values in the sequence.
"""
columns: List[ProgressColumn] = (
[TextColumn("[progress.description]{task.description}")] if description else []
)
columns.extend(
(
BarColumn(
style=style,
complete_style=complete_style,
finished_style=finished_style,
pulse_style=pulse_style,
),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
"{task.completed}/{task.total}",
TimeRemainingColumn(),
">",
TimeElapsedColumn(),
)
)
progress = Progress(
*columns,
auto_refresh=auto_refresh,
console=console,
transient=transient,
get_time=get_time,
refresh_per_second=refresh_per_second or 10,
disable=disable,
)
return progress
def track(
sequence: Union[Sequence[ProgressType], Iterable[ProgressType]],
description="Working...",
total: int = None,
auto_refresh=True,
console: Optional[Console] = None,
transient: bool = False,
get_time: Callable[[], float] = None,
refresh_per_second: float = 10,
style: StyleType = "bar.back",
complete_style: StyleType = "bar.complete",
finished_style: StyleType = "bar.finished",
pulse_style: StyleType = "bar.pulse",
update_period: float = 0.1,
disable: bool = False,
) -> Iterable[ProgressType]:
"""Track progress by iterating over a sequence.
Args:
sequence (Iterable[ProgressType]): A sequence (must support "len") you wish to iterate over.
description (str, optional): Description of task show next to progress bar. Defaults to "Working".
total: (int, optional): Total number of steps. Default is len(sequence).
auto_refresh (bool, optional): Automatic refresh, disable to force a refresh after each iteration. Default is True.
transient: (bool, optional): Clear the progress on exit. Defaults to False.
console (Console, optional): Console to write to. Default creates internal Console instance.
refresh_per_second (float): Number of times per second to refresh the progress information. Defaults to 10.
style (StyleType, optional): Style for the bar background. Defaults to "bar.back".
complete_style (StyleType, optional): Style for the completed bar. Defaults to "bar.complete".
finished_style (StyleType, optional): Style for a finished bar. Defaults to "bar.done".
pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.
disable (bool, optional): Disable display of progress.
Returns:
Iterable[ProgressType]: An iterable of the values in the sequence.
"""
columns: List["ProgressColumn"] = (
[TextColumn("[progress.description]{task.description}")] if description else []
)
columns.extend(
(
BarColumn(
style=style,
complete_style=complete_style,
finished_style=finished_style,
pulse_style=pulse_style,
),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
"{task.completed}/{task.total}",
TimeRemainingColumn(),
">",
TimeElapsedColumn(),
)
)
progress = Progress(
*columns,
auto_refresh=auto_refresh,
console=console,
transient=transient,
get_time=get_time,
refresh_per_second=refresh_per_second or 10,
disable=disable,
)
with progress:
yield from progress.track(
sequence, total=total, description=description, update_period=update_period
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment