Created
June 28, 2020 20:23
-
-
Save ian-whitestone/51978be5bd6093325f7656a14f4461c0 to your computer and use it in GitHub Desktop.
A simple progress bar that just shows the total number of "things" processed and the time elapsed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A simple progress bar that just shows the total number of "things" processed | |
and the time elapsed | |
""" | |
import time | |
from datetime import timedelta | |
from rich.progress import Progress, ProgressColumn, Task, TaskID, Text, TextColumn | |
class TimeElapsedColumn(ProgressColumn): | |
"""Renders time elapsed.""" | |
# Only refresh twice a second to prevent jitter | |
max_refresh = 0.5 | |
def render(self, task: "Task") -> Text: | |
"""Show time elapsed.""" | |
elapsed = task.elapsed | |
if elapsed is None: | |
return Text("-:--:--", style="progress.remaining") | |
elapsed_delta = timedelta(seconds=int(elapsed)) | |
return Text(str(elapsed_delta), style="progress.remaining") | |
progress = Progress( | |
TextColumn("[bold black]{task.description}", justify="right",), | |
"•", | |
TextColumn( | |
"[bold red]{task.fields[completed_description]} {task.completed}", | |
justify="right", | |
), | |
"•", | |
TimeElapsedColumn(), | |
) | |
def run(task_id: TaskID) -> None: | |
progress.update(task_id, total=100) | |
progress.start_task(task_id) | |
for x in range(0, 50): | |
time.sleep(0.1) | |
progress.update(task_id, advance=1) | |
def download(): | |
with progress: | |
task_id = progress.add_task( | |
"Downloading rows", completed_description="Records processed", start=False | |
) | |
run(task_id) | |
print("now doing other stuff") | |
time.sleep(3) | |
print("Done") | |
if __name__ == "__main__": | |
download() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment