Is There a Way for Your Python Script to Display a Percent Bar for Uploading

Downloading files with Python is super easy and can even be syntactically uncomplicated with robust libraries similar requests. However, in that location is no standard library implementation to bear witness a progress bar in python during a download. In this quick tutorial, we'll show how to chop-chop implement such a feature to help avert guesswork during long downloads.

  1. 1 Introduction: Understanding the Problem
  2. ii Step 1: Preparing an HTTP Asking
  3. iii Steps 2 & 3: Metering & Displaying the Transfer of Data
    1. 3.ane Manual Implementation
    2. 3.ii tqdm Implementation
  4. four Last Thoughts

Introduction: Understanding the Problem

Before we swoop in we need to understand all the moving pieces of this trouble. This volition provide the framework by which nosotros can contrive our approach of showing a progress bar while downloading a file via Python. An outline is as follows:

  1. Make an HTTP request to download a file
  2. Metering the transfer of information from the remote server
  3. Displaying the metering process on the panel

The adept news is that we don't accept to implement any of this from scratch—or fifty-fifty on a low-enough level that we would need to bear upon Python'southward urllib. Pace i will be handled via the Python requests library and both steps two and 3 will exist handled via the tqdm library. Let'south get started by because downloading our file.

Step one: Preparing an HTTP Request

To integrate a progress bar into Python while downloading a file, we need to change the approach that we might take otherwise. Let'due south start by considering how one might download a file in Python without using a progress bar. The post-obit code will download a file and relieve it to the local file organisation:

import requests  with requests.get('https://www.example.com/file.txt')every bit r:     with open('download.txt', 'wb')every bit file:         file.write(r.raw)

This code uses the requests library to construct an HTTP Get request to the https://www.example.com/file.txt URL. This will download the content of example.com and save it as a file named download.txt on the local disk—46 lines of HTML code.

Notation: Check out this commodity on other uncomplicated ways to download a file with Python.

Steps ii & 3: Metering & Displaying the Transfer of Data

In the example in a higher place, nosotros download the entirety of a remote file and salve its contents to a newly-created local file. This doesn't let for any incremental measuring—the file is downloaded entirely and then saved entirely. Let's first look at how we might implement this manually, and so nosotros'll expect at a more streamlined approach.

Manual Implementation

To incrementalize the download process, such that nosotros tin can register the progress, we need to set the stream parameter of our requests object to True. This sends an initial HEAD asking to get file information which will and so allow iteration of transferred bytes. This is implemented equally such:

import time import requests  # use a context managing director to make an HTTP request and file with requests.become("https://world wide web.instance.com/file.txt", stream=True) every bit r:     with open up('download.txt', 'wb') equally file:          # Get the full size, in bytes, from the response header         total_size = int(r.headers.become('Content-Length'))          # Define the size of the chunk to iterate over (Mb)         chunk_size = 1          # iterate over every chunk and calculate % of total         for i, chunk in enumerate(r.iter_content(chunk_size=chunk_size)):              # calculate current pct             c = i * chunk_size / total_size * 100              # write current % to panel, pause for .1ms, then flush panel             sys.stdout.write(f"\r{circular(c, 4)}%")             time.sleep(.ane)             sys.stdout.affluent()

When this lawmaking executes, the same data equally before is downloaded, but at present we are iterating over the byte stream from the response. While doing this, we are calculating the full percentage of bytes downloaded—relative to the total number conveyed by the Content-Length header from our initial HTTP request. This will proceed to display a value like to 5.234234% in the console every .1ms.

The only real magic here is using the sys.stdout.write method to stream to the console, the time.sleep method to pause for .1 millisecond, and so the sys.stdout.flush method to move the cursor back to the kickoff character. This is sloppy, could be much-improved even in its electric current arroyo, and adds a .1ms delay for each chunk size—which is a single byte in the to a higher place example. Let's expect at a more streamlined approach.

tqdm Implementation

The tqdm library for Python provides a much more syntactically curtailed means of displaying download progress in the panel. This library can be used to monitor and display the progress of whatsoever iterable procedure. In that location are several approaches this library provides, including command line usage, but here we'll implement via the wrapattr convenience office as such:

import requests import shutil from tqdm.auto import tqdm  # make an HTTP request within a context director with requests.go("https://www.example.com/file.txt", stream=True) as r:          # bank check header to get content length, in bytes     total_length = int(r.headers.get("Content-Length"))          # implement progress bar via tqdm     with tqdm.wrapattr(r.raw, "read", full=total_length, desc="")as raw:              # save the output to a file         with open up(f"{os.path.basename(r.url)}", 'wb')as output:             shutil.copyfileobj(raw, output)

The apply of context managers hither is discretionary, with the request.become call acceptably done without such—call it a force of habit. This code volition download the same data into the aforementioned file as before, except at present we'll be shown the following on the console:

100%|██████████| 648/648 [00:00<00:00, 324kB/southward]

The use of the tqdm module here provides u.s. with an aesthetically-pleasing progress bar with the following data:

  1. Overall progress as a percentage
  2. Dynamically-updated progress bar
  3. total bytes downloaded/total bytes available
  4. total elapsed time
  5. data transfer speed (kB/s)

The size of the file beingness downloaded here (648 bytes) hardly allows a full demonstration of the tqdm progress bar feel. After re-running the to a higher place code with the URL of a podcast episode specified, the download bar is better illustrated as such:

6%|▌         | thirteen.0M/212M [00:04<01:04, iii.21MB/s]

Hither we come across that a download is 6% of the style completed in downloaded a total of 212M later on 4 seconds of an estimated 1:04 total look time averaging a rate of iii.21MB/s. This type of feedback is invaluable during the initial development and logging of circuitous applications.

Final Thoughts

Downloading files in Python is a simple job. Tacking on a robust library like the requests library makes it fifty-fifty easier. There is no curtailed way to brandish the progress of a file download to the console in Python.

Our initial manual implementation gets the job done but could utilise an industrial-strength dose of consideration for formatting. The tqdm module has a relatively light footprint and requires only the colorama library equally a dependency. That does allow one to customize the color of the display text—something we didn't bear upon hither.

alpharithms discord banner 1

brunohimst1949.blogspot.com

Source: https://www.alpharithms.com/progress-bars-for-python-downloads-580122/

Belum ada Komentar untuk "Is There a Way for Your Python Script to Display a Percent Bar for Uploading"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel