@@ -115,6 +115,7 @@ def track(
115
115
pulse_style : StyleType = "bar.pulse" ,
116
116
update_period : float = 0.1 ,
117
117
disable : bool = False ,
118
+ show_speed : bool = True ,
118
119
) -> Iterable [ProgressType ]:
119
120
"""Track progress by iterating over a sequence.
120
121
@@ -132,6 +133,7 @@ def track(
132
133
pulse_style (StyleType, optional): Style for pulsing bars. Defaults to "bar.pulse".
133
134
update_period (float, optional): Minimum time (in seconds) between calls to update(). Defaults to 0.1.
134
135
disable (bool, optional): Disable display of progress.
136
+ show_speed (bool, optional): Show speed if total isn't known. Defaults to True.
135
137
Returns:
136
138
Iterable[ProgressType]: An iterable of the values in the sequence.
137
139
@@ -148,7 +150,7 @@ def track(
148
150
finished_style = finished_style ,
149
151
pulse_style = pulse_style ,
150
152
),
151
- TaskProgressColumn (),
153
+ TaskProgressColumn (show_speed = show_speed ),
152
154
TimeRemainingColumn (),
153
155
)
154
156
)
@@ -676,7 +678,18 @@ def render(self, task: "Task") -> Text:
676
678
677
679
678
680
class TaskProgressColumn (TextColumn ):
679
- """A column displaying the progress of a task."""
681
+ """Show task progress as a percentage.
682
+
683
+ Args:
684
+ text_format (str, optional): Format for percentage display. Defaults to "[progress.percentage]{task.percentage:>3.0f}%".
685
+ text_format_no_percentage (str, optional): Format if percentage is unknown. Defaults to "".
686
+ style (StyleType, optional): Style of output. Defaults to "none".
687
+ justify (JustifyMethod, optional): Text justification. Defaults to "left".
688
+ markup (bool, optional): Enable markup. Defaults to True.
689
+ highlighter (Optional[Highlighter], optional): Highlighter to apply to output. Defaults to None.
690
+ table_column (Optional[Column], optional): Table Column to use. Defaults to None.
691
+ show_speed (bool, optional): Show speed if total is unknown. Defaults to False.
692
+ """
680
693
681
694
def __init__ (
682
695
self ,
@@ -687,8 +700,11 @@ def __init__(
687
700
markup : bool = True ,
688
701
highlighter : Optional [Highlighter ] = None ,
689
702
table_column : Optional [Column ] = None ,
703
+ show_speed : bool = False ,
690
704
) -> None :
705
+
691
706
self .text_format_no_percentage = text_format_no_percentage
707
+ self .show_speed = show_speed
692
708
super ().__init__ (
693
709
text_format = text_format ,
694
710
style = style ,
@@ -698,7 +714,29 @@ def __init__(
698
714
table_column = table_column ,
699
715
)
700
716
717
+ @classmethod
718
+ def render_speed (cls , speed : Optional [float ]) -> Text :
719
+ """Render the speed in iterations per second.
720
+
721
+ Args:
722
+ task (Task): A Task object.
723
+
724
+ Returns:
725
+ Text: Text object containing the task speed.
726
+ """
727
+ if speed is None :
728
+ return Text ("" , style = "progress.percentage" )
729
+ unit , suffix = filesize .pick_unit_and_suffix (
730
+ int (speed ),
731
+ ["" , "×10³" , "×10⁶" , "×10⁹" , "×10¹²" ],
732
+ 1000 ,
733
+ )
734
+ data_speed = speed / unit
735
+ return Text (f"{ data_speed :.1f} { suffix } it/s" , style = "progress.percentage" )
736
+
701
737
def render (self , task : "Task" ) -> Text :
738
+ if task .total is None and self .show_speed :
739
+ return self .render_speed (task .finished_speed or task .speed )
702
740
text_format = (
703
741
self .text_format_no_percentage if task .total is None else self .text_format
704
742
)
@@ -1152,13 +1190,10 @@ def track(
1152
1190
Iterable[ProgressType]: An iterable of values taken from the provided sequence.
1153
1191
"""
1154
1192
1193
+ task_total : Optional [float ] = None
1155
1194
if total is None :
1156
1195
if isinstance (sequence , Sized ):
1157
1196
task_total = float (len (sequence ))
1158
- else :
1159
- raise ValueError (
1160
- f"unable to get size of { sequence !r} , please specify 'total'"
1161
- )
1162
1197
else :
1163
1198
task_total = total
1164
1199
0 commit comments