Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: Installation progress bar ✨
Installation can be pretty slow so it'd be nice to provide progress
feedback to the user.

This commit adds a new progress renderer designed for installation:

- The progress bar will wait one refresh cycle (1000ms/6 = 170ms) before
  appearing. This avoids unsightly very short flashes.

- The progress bar is transient (i.e. it will disappear once all
  packages have been installed). This choice was made to avoid adding
  more clutter to pip install's output (despite the download progress
  bar being persistent).

- The progress bar won't be used at all if there's only one package to
  install.
  • Loading branch information
ichard26 committed Feb 12, 2025
commit 24e364eb7366c263218876beeefae7897a4d08c8
1 change: 1 addition & 0 deletions news/12712.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Display a transient progress bar during package installation.
51 changes: 45 additions & 6 deletions src/pip/_internal/cli/progress_bars.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import functools
import sys
from typing import Callable, Generator, Iterable, Iterator, Optional, Tuple
from typing import Callable, Generator, Iterable, Iterator, Optional, Tuple, TypeVar

from pip._vendor.rich.progress import (
BarColumn,
DownloadColumn,
FileSizeColumn,
MofNCompleteColumn,
Progress,
ProgressColumn,
SpinnerColumn,
Expand All @@ -16,12 +17,14 @@
)

from pip._internal.cli.spinners import RateLimiter
from pip._internal.utils.logging import get_indentation
from pip._internal.req.req_install import InstallRequirement
from pip._internal.utils.logging import get_console, get_indentation

DownloadProgressRenderer = Callable[[Iterable[bytes]], Iterator[bytes]]
T = TypeVar("T")
ProgressRenderer = Callable[[Iterable[T]], Iterator[T]]


def _rich_progress_bar(
def _rich_download_progress_bar(
iterable: Iterable[bytes],
*,
bar_type: str,
Expand Down Expand Up @@ -57,6 +60,28 @@ def _rich_progress_bar(
progress.update(task_id, advance=len(chunk))


def _rich_install_progress_bar(
iterable: Iterable[InstallRequirement], *, total: int
) -> Iterator[InstallRequirement]:
columns = (
TextColumn("{task.fields[indent]}"),
BarColumn(),
MofNCompleteColumn(),
TextColumn("{task.description}"),
)
console = get_console()

bar = Progress(*columns, refresh_per_second=6, console=console, transient=True)
# Hiding the progress bar at initialization forces a refresh cycle to occur
# until the bar appears, avoiding very short flashes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice trick!

task = bar.add_task("", total=total, indent=" " * get_indentation(), visible=False)
with bar:
for req in iterable:
bar.update(task, description=rf"\[{req.name}]", visible=True)
yield req
bar.advance(task)


def _raw_progress_bar(
iterable: Iterable[bytes],
*,
Expand All @@ -81,14 +106,28 @@ def write_progress(current: int, total: int) -> None:

def get_download_progress_renderer(
*, bar_type: str, size: Optional[int] = None
) -> DownloadProgressRenderer:
) -> ProgressRenderer[bytes]:
"""Get an object that can be used to render the download progress.

Returns a callable, that takes an iterable to "wrap".
"""
if bar_type == "on":
return functools.partial(_rich_progress_bar, bar_type=bar_type, size=size)
return functools.partial(
_rich_download_progress_bar, bar_type=bar_type, size=size
)
elif bar_type == "raw":
return functools.partial(_raw_progress_bar, size=size)
else:
return iter # no-op, when passed an iterator


def get_install_progress_renderer(
*, bar_type: str, total: int
) -> ProgressRenderer[InstallRequirement]:
"""Get an object that can be used to render the install progress.
Returns a callable, that takes an iterable to "wrap".
"""
if bar_type == "on":
return functools.partial(_rich_install_progress_bar, total=total)
else:
return iter
1 change: 1 addition & 0 deletions src/pip/_internal/commands/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ def run(self, options: Values, args: List[str]) -> int:
warn_script_location=warn_script_location,
use_user_site=options.use_user_site,
pycompile=options.compile,
progress_bar=options.progress_bar,
)

lib_locations = get_lib_location_guesses(
Expand Down
15 changes: 14 additions & 1 deletion src/pip/_internal/req/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dataclasses import dataclass
from typing import Generator, List, Optional, Sequence, Tuple

from pip._internal.cli.progress_bars import get_install_progress_renderer
from pip._internal.utils.logging import indent_log

from .req_file import parse_requirements
Expand Down Expand Up @@ -41,6 +42,7 @@ def install_given_reqs(
warn_script_location: bool,
use_user_site: bool,
pycompile: bool,
progress_bar: str,
) -> List[InstallationResult]:
"""
Install everything in the given list.
Expand All @@ -57,8 +59,19 @@ def install_given_reqs(

installed = []

show_progress = logger.isEnabledFor(logging.INFO) and len(to_install) > 1

items = iter(to_install.values())
if show_progress:
renderer = get_install_progress_renderer(
bar_type=progress_bar, total=len(to_install)
)
items = renderer(items)

with indent_log():
for req_name, requirement in to_install.items():
for requirement in items:
req_name = requirement.name
assert req_name is not None
if requirement.should_reinstall:
logger.info("Attempting uninstall: %s", req_name)
with indent_log():
Expand Down
Loading