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.
Using progress bars of pip
Asked Answered
Souce code of pip. It's probably in _vendor/progress and it's used in _internal/cli –
Ervinervine
The answers below are outdated. Pip now has a nicer progress bar with color. more info here: #71924204 –
Maillot
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/
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 PyPI –
Reitareiter Look under rich progress bar
:)
Very nice, but is that what pip uses? –
Jareb
Here, you can use tqdm. And you can customize it to any extent.
but it needs to be installed which I don't want to –
Rightism
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.py –
Barbicel
is there a way to import and use its progress bars –
Rightism
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 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
© 2022 - 2024 — McMap. All rights reserved.