Skip to content

Commit

Permalink
Add beautiful progressbar (#139)
Browse files Browse the repository at this point in the history
* Add beautiful progressbar

* minor refinement
  • Loading branch information
zhouzaida committed Jun 24, 2022
1 parent d2df7c8 commit 90d97cd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
2 changes: 2 additions & 0 deletions mim/utils/__init__.py
Expand Up @@ -9,6 +9,7 @@
USER,
WHEEL_URL,
)
from .progress_bars import rich_progress_bar
from .utils import (
args2string,
call_command,
Expand Down Expand Up @@ -86,4 +87,5 @@
'get_package_info_from_pypi',
'parse_home_page',
'ensure_installation',
'rich_progress_bar',
]
37 changes: 37 additions & 0 deletions mim/utils/progress_bars.py
@@ -0,0 +1,37 @@
# Copyright (c) OpenMMLab. All rights reserved.
# Copyright (c) https://github.com/pypa/pip
# Modified from https://github.com/pypa/pip/blob/main/src/pip/_internal/cli/progress_bars.py # noqa: E501

from typing import Callable, Generator, Iterable, Iterator

from rich.progress import (
BarColumn,
DownloadColumn,
Progress,
TextColumn,
TimeRemainingColumn,
TransferSpeedColumn,
)

DownloadProgressRenderer = Callable[[Iterable[bytes]], Iterator[bytes]]


def rich_progress_bar(
iterable: Iterable[bytes],
size: int,
) -> Generator[bytes, None, None]:
columns = (
TextColumn('[progress.description]{task.description}'),
BarColumn(),
DownloadColumn(binary_units=1024),
TransferSpeedColumn(),
TextColumn('eta'),
TimeRemainingColumn(),
)

progress = Progress(*columns, refresh_per_second=30)
task_id = progress.add_task('downloading', total=size)
with progress:
for chunk in iterable:
yield chunk
progress.update(task_id, advance=len(chunk))
14 changes: 7 additions & 7 deletions mim/utils/utils.py
Expand Up @@ -21,6 +21,7 @@
from requests.models import Response

from .default import PKG2PROJECT
from .progress_bars import rich_progress_bar


def parse_url(url: str) -> Tuple[str, str]:
Expand Down Expand Up @@ -172,13 +173,12 @@ def download_from_file(url: str,
size = int(response.headers.get('content-length'))
with open(dest_path, 'wb') as fw:
content_iter = response.iter_content(chunk_size=1024)
with click.progressbar(content_iter, length=size / 1024) as chunks:
for chunk in chunks:
if chunk:
fw.write(chunk)
fw.flush()
if hash_prefix is not None:
sha256.update(chunk)
for chunk in rich_progress_bar(content_iter, size=size):
if chunk:
fw.write(chunk)
fw.flush()
if hash_prefix is not None:
sha256.update(chunk)

if hash_prefix is not None:
digest = sha256.hexdigest()
Expand Down
1 change: 1 addition & 0 deletions requirements/install.txt
Expand Up @@ -4,4 +4,5 @@ model-index
pandas
pip>=19.3
requests
rich
tabulate

0 comments on commit 90d97cd

Please sign in to comment.