]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Progress examples (#1272)
authorTom Christie <tom@tomchristie.com>
Thu, 10 Sep 2020 11:28:08 +0000 (12:28 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Sep 2020 11:28:08 +0000 (12:28 +0100)
* Progress examples

* Update advanced.md

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
docs/advanced.md
docs/img/rich-progress.gif [new file with mode: 0644]
docs/img/tqdm-progress.gif [new file with mode: 0644]

index d9a43354fa760d0e4123b2b032e6f58a61796ab5..b0ed25fe1c6a9bb6ffd61561c2c962e5b674ab84 100644 (file)
@@ -246,9 +246,36 @@ with tempfile.NamedTemporaryFile() as download_file:
                 download_file.write(chunk)
                 progress.update(response.num_bytes_downloaded - num_bytes_downloaded)
                 num_bytes_downloaded = response.num_bytes_downloaded
-        print(f"The total download size is {response.num_bytes_downloaded} bytes")
 ```
 
+![tqdm progress bar](img/tqdm-progress.gif)
+
+Or an alternate example, this time using the [`rich`](https://github.com/willmcgugan/rich) library…
+
+```python
+import tempfile
+import httpx
+import rich.progress
+
+with tempfile.NamedTemporaryFile() as download_file:
+    url = "https://speed.hetzner.de/100MB.bin"
+    with httpx.stream("GET", url) as response:
+        total = int(response.headers["Content-Length"])
+
+        with rich.progress.Progress(
+            "[progress.percentage]{task.percentage:>3.0f}%",
+            rich.progress.BarColumn(bar_width=None),
+            rich.progress.DownloadColumn(),
+            rich.progress.TransferSpeedColumn(),
+        ) as progress:
+            download_task = progress.add_task("Download", total=total)
+            for chunk in response.iter_bytes():
+                download_file.write(chunk)
+                progress.update(download_task, completed=response.num_bytes_downloaded)
+```
+
+![rich progress bar](img/rich-progress.gif)
+
 ## .netrc Support
 
 HTTPX supports .netrc file. In `trust_env=True` cases, if auth parameter is
diff --git a/docs/img/rich-progress.gif b/docs/img/rich-progress.gif
new file mode 100644 (file)
index 0000000..7c1a858
Binary files /dev/null and b/docs/img/rich-progress.gif differ
diff --git a/docs/img/tqdm-progress.gif b/docs/img/tqdm-progress.gif
new file mode 100644 (file)
index 0000000..7a3b0a8
Binary files /dev/null and b/docs/img/tqdm-progress.gif differ