Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'completed' parameter to 'track' function for resumable progress #3220

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions rich/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def track(
sequence: Union[Sequence[ProgressType], Iterable[ProgressType]],
description: str = "Working...",
total: Optional[float] = None,
completed: int = 0,
auto_refresh: bool = True,
console: Optional[Console] = None,
transient: bool = False,
Expand All @@ -123,6 +124,7 @@ def track(
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: (float, optional): Total number of steps. Default is len(sequence).
completed (int, optional): Number of steps completed so far. Defaults to 0.
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.
Expand Down Expand Up @@ -166,7 +168,7 @@ def track(

with progress:
yield from progress.track(
sequence, total=total, description=description, update_period=update_period
sequence, total=total, completed=completed, description=description, update_period=update_period
)


Expand Down Expand Up @@ -1180,6 +1182,7 @@ def track(
self,
sequence: Union[Iterable[ProgressType], Sequence[ProgressType]],
total: Optional[float] = None,
completed: int = 0,
task_id: Optional[TaskID] = None,
description: str = "Working...",
update_period: float = 0.1,
Expand All @@ -1189,6 +1192,7 @@ def track(
Args:
sequence (Sequence[ProgressType]): A sequence of values you want to iterate over and track progress.
total: (float, optional): Total number of steps. Default is len(sequence).
completed (int, optional): Number of steps completed so far. Defaults to 0.
task_id: (TaskID): Task to track. Default is new task.
description: (str, optional): Description of task, if new task is created.
update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.
Expand All @@ -1200,9 +1204,9 @@ def track(
total = float(length_hint(sequence)) or None

if task_id is None:
task_id = self.add_task(description, total=total)
task_id = self.add_task(description, total=total, completed=completed)
else:
self.update(task_id, total=total)
self.update(task_id, total=total, completed=completed)

if self.live.auto_refresh:
with _TrackThread(self, task_id, update_period) as track_thread:
Expand Down