Using progress bars of pip
Asked Answered
R

4

12

I want use progress bars in my python code. I know there are many libraries for that but I want to use the progress bars used by pip [the package manager]. Please tell if there is a way to do this.

Rightism answered 27/10, 2020 at 8:14 Comment(2)
Souce code of pip. It's probably in _vendor/progress and it's used in _internal/cliErvinervine
The answers below are outdated. Pip now has a nicer progress bar with color. more info here: #71924204Maillot
R
9

The progress package available on pypi is used by pip. It can be imported by including the following line in your python file:

from pip._vendor import progress

Usage is available on https://pypi.org/project/progress/

Rightism answered 10/11, 2020 at 9:15 Comment(2)
This does not seem to work anymore (pip 22.1), the pip progress bar is now under pip._vendor.rich.progress.Balladmonger
And that indicates that pip is now using a progress bar from rich, which is also on PyPIReitareiter
P
9

Look under rich progress bar :)

Protonema answered 30/9, 2022 at 13:31 Comment(1)
Very nice, but is that what pip uses?Jareb
B
5

Here, you can use tqdm. And you can customize it to any extent.

https://pypi.org/project/tqdm/

Barbicel answered 27/10, 2020 at 8:22 Comment(5)
but it needs to be installed which I don't want toRightism
pip uses it's own progress bar (their own source code), check their codebase: github.com/pypa/pip/blob/master/src/pip/_vendor/progress/bar.pyBarbicel
is there a way to import and use its progress barsRightism
Yes (would be quite complex though), depends on how you plan to use it. Take the inspiration from the following codebase github.com/pypa/pip/blob/master/src/pip/_internal/cli/…. This will be an uphill task.Barbicel
I found that pip internally uses progress for its progress bars. They have copied the whole package inside the _vendor. And there are many more there.Rightism
R
0

I struggle a little bit to make it work, so for the noobs like me, here is my part.

https://pypi.org/project/progress/ says : from progress.bar import Bar , but there is no bar module there.

I did :

from pip._vendor.rich.progress import Progress

For my use case, which is having an infinite progress bar while i wait my file to be downloaded, i use it like that.

def wait_for_download_finish(file_name,folder):

    " Create a spinning bar until download is finished"

    # Initialize the progress bar 
    bar = Progress()
    # Set a description, and make the bar progress indefinitly
    id = bar.add_task(description="Downloading Files ...",total=None)
    # Start the progress bar
    bar.start()

    # We look for the matching string of the file we wait in the latest file in folder
    latest_file = []
    while file_name not in latest_file:
        # List all the files in folder
        list_of_files = glob.glob(folder + "/*",include_hidden=True, recursive=False) # * means for all. If you need specific format, use *.mkv
        # Get the latest file
        latest_file = max(list_of_files, key=os.path.getctime)
        # Keep the bar going
        bar.advance(id)
        # Add some delay
        time.sleep(1)

    # Stop the progress bar.
    bar.stop()

    print("Download finished!")
    return

# Use the os.path.expand if you want to use the "~"  in your path
wait_for_download_finish("test", os.path.expanduser("~"))

By the way i discovered the rich library. It's awesome

Reviviscence answered 13/3 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.