You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1-4
Original file line number
Diff line number
Diff line change
@@ -5,14 +5,11 @@ All notable changes to this project will be documented in this file.
5
5
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
8
-
## 12.1.0
8
+
## [12.1.0] - 2022-04-03
9
9
10
10
### Added
11
11
12
12
- Progress.open and Progress.wrap_file method to track the progress while reading from a file or file-like object https://github.com/willmcgugan/rich/pull/1759
Copy file name to clipboardExpand all lines: docs/source/progress.rst
+20-19
Original file line number
Diff line number
Diff line change
@@ -212,37 +212,38 @@ If the :class:`~rich.progress.Progress` class doesn't offer exactly what you nee
212
212
Reading from a file
213
213
~~~~~~~~~~~~~~~~~~~
214
214
215
-
You can obtain a progress-tracking reader using the :meth:`~rich.progress.Progress.open` method by giving it a path. You can specify the number of bytes to be read, but by default :meth:`~rich.progress.Progress.open` will query the size of the file with :func:`os.stat`. You are responsible for closing the file, and you should consider using a *context* to make sure it is closed ::
215
+
Rich provides an easy way to generate a progress bar for reading a file. If you call :func:`~rich.progress.open` it will return a context manager which displays a progress bar while you read. This is particularly useful when you can't easily modify the code that does the reading.
216
+
217
+
The following example shows how we might show progress for reading a JSON file::
216
218
217
219
import json
218
-
from rich.progress import Progress
220
+
import rich.progress
219
221
220
-
with Progress() as progress:
221
-
with progress.open("data.json", "rb") as file:
222
-
json.load(file)
222
+
with rich.progress.open("data.json", "rb") as file:
223
+
data = json.load(file)
224
+
print(data)
223
225
226
+
If you already have a file object, you can call :func:`~rich.progress.wrap_file` which returns a context manager that wraps your file so that it displays a progress bar. If you use this function you will need to set the number of bytes or characters you expect to read.
224
227
225
-
Note that in the above snippet we use the `"rb"` mode, because we needed the file to be opened in binary mode to pass it to :func:`json.load`. If the API consuming the file is expecting an object in *text mode* (for instance, :func:`csv.reader`), you can open the file with the `"r"` mode, which happens to be the default ::
228
+
Here's an example that reads a url from the internet::
226
229
227
-
from rich.progress import Progress
230
+
from time import sleep
231
+
from urllib.request import urlopen
228
232
229
-
with Progress() as progress:
230
-
with progress.open("README.md") as file:
231
-
for line in file:
232
-
print(line)
233
+
from rich.progress import wrap_file
233
234
235
+
response = urlopen("https://www.textualize.io")
236
+
size = int(response.headers["Content-Length"])
234
237
235
-
Reading from a file-like object
236
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
238
+
with wrap_file(response, size) as file:
239
+
for line in file:
240
+
print(line.decode("utf-8"), end="")
241
+
sleep(0.1)
237
242
238
-
You can obtain a progress-tracking reader wrapping a file-like object using the :meth:`~rich.progress.Progress.wrap_file` method. The file-like object must be in *binary mode*, and a total must be provided, unless it was provided to a :class:`~rich.progress.Task` created beforehand. The returned reader may be used in a context, but will not take care of closing the wrapped file ::
239
243
240
-
import json
241
-
from rich.progress import Progress
244
+
If you expect to be reading from multiple files, you can use :meth:`~rich.progress.Progress.open` or :meth:`~rich.progress.Progress.wrap_file` to add a file progress to an existing Progress instance.
242
245
243
-
with Progress() as progress:
244
-
with open("data.json", "rb") as file:
245
-
json.load(progress.wrap_file(file, total=2048))
246
+
See `cp_progress.py <https://github.com/willmcgugan/rich/blob/master/examples/cp_progress.py>` for a minimal clone of the ``cp`` command which shows a progress bar as the file is copied.
0 commit comments