From 056e0f743c10e6217d77d099cf9af99d96c48c8b Mon Sep 17 00:00:00 2001 From: Olaf Gladis Date: Wed, 20 Apr 2022 16:17:51 +0200 Subject: [PATCH] Update progress.rst fix #2204 The docs are still mentioning `rich.progress.read` but the new way is `rich.progress.open` I move the docs handling files to top. I you think progress for reading a file is not basic usage please decline this PR. To have a progress when reading a file is very useful for me, and I personally think this it is a great selling point. --- docs/source/progress.rst | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/docs/source/progress.rst b/docs/source/progress.rst index 8d4f81b2a..aea04fc2a 100644 --- a/docs/source/progress.rst +++ b/docs/source/progress.rst @@ -27,13 +27,16 @@ For basic usage call the :func:`~rich.progress.track` function, which accepts a do_work(n) -To get a progress bar while reading from a file, you may consider using the :func:`~rich.progress.read` function, which accepts a path, or a *file-like* object. It will return a *file-like* object in *binary mode* that will update the progress information as it's being read from. Here's an example, tracking the progresses made by :func:`json.load` to load a file:: +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. + +The following example shows how we might show progress for reading a JSON file:: import json - from rich.progress import read + import rich.progress - with read("data.json", description="Loading data...") as f: - data = json.load(f) + with rich.progress.open("data.json", "rb") as file: + data = json.load(file) + print(data) Advanced usage @@ -209,19 +212,8 @@ If the :class:`~rich.progress.Progress` class doesn't offer exactly what you nee def get_renderables(self): yield Panel(self.make_tasks_table(self.tasks)) -Reading from a file -~~~~~~~~~~~~~~~~~~~ - -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. - -The following example shows how we might show progress for reading a JSON file:: - - import json - import rich.progress - - with rich.progress.open("data.json", "rb") as file: - data = json.load(file) - print(data) +Wrapping a file +~~~~~~~~~~~~~~~ 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.