progress docs

This commit is contained in:
Will McGugan 2022-04-01 14:49:02 +01:00
parent 56227ab383
commit 8745ee5a11
4 changed files with 45 additions and 3 deletions

View File

@ -212,7 +212,7 @@ If the :class:`~rich.progress.Progress` class doesn't offer exactly what you nee
Reading from a file 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. 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:: The following example shows how we might show progress for reading a JSON file::
@ -223,7 +223,7 @@ The following example shows how we might show progress for reading a JSON file::
data = json.load(file) data = json.load(file)
print(data) print(data)
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 generates a progress bar. If you use this function you will need to set the number of bytes or characters you expect to read. 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.
Here's an example that reads a url from the internet:: Here's an example that reads a url from the internet::

16
examples/file_progress.py Normal file
View File

@ -0,0 +1,16 @@
from time import sleep
from urllib.request import urlopen
from rich.progress import wrap_file
# Read a URL with urlopen
response = urlopen("https://www.textualize.io")
# Get the size from the headers
size = int(response.headers["Content-Length"])
# Wrap the response so that it update progress
with wrap_file(response, size) as file:
for line in file:
print(line.decode("utf-8"), end="")
sleep(0.1)

View File

@ -0,0 +1,26 @@
"""
Demonstrates how to export a SVG
"""
from rich.console import Console
from rich.table import Table
table = Table(title="Star Wars Movies")
table.add_column("Released", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
table.add_column("Box Office", justify="right", style="green")
table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
console = Console(record=True)
console.print(table, justify="center")
console.save_svg("table.svg", title="save_table_svg.py")
import os
import webbrowser
webbrowser.open(f"file://{os.path.abspath('table.svg')}")

View File

@ -2350,7 +2350,7 @@ class Console:
) )
) )
fragments = [] fragments: List[str] = []
theme_foreground_color = _theme.foreground_color.hex theme_foreground_color = _theme.foreground_color.hex
theme_background_color = _theme.background_color.hex theme_background_color = _theme.background_color.hex