Python Rich is getting richer
Yep, you heard it right! Python Rich is getting richer. Do you remember when I had explained how you can get rich using Python? Nope? Well, go and read that first here.๐
Read it? Now you might have understood what I am talking about. Yes, the Rich library has been updated with two new interesting features. Without further ado, let's discuss them now.
SVG Support
In addition to HTML and Text, Rich now supports SVG too. It can now export console output to an SVG file when captured. These SVGs are quite useful when writing documentation, or for bloggers like me where we need to share the console output.
Let us see how we can do it.
from rich.console import Console
from rich.table import Table
import requests
## Creating table
table = Table(title="iRead Blogs")
## Adding Columns
table.add_column("ID", style="cyan", no_wrap=True)
table.add_column("Post Title", style="magenta")
table.add_column("Author", justify="center", style="green")
## Getting data
response = requests.get("https://ireadblog.com/api/v1/posts?limit=5").json()
## Adding Rows
for post in response:
table.add_row(str(post["pk"]), post["fields"]["title"], post["fields"]["author"])
## Console
console = Console(record=True)
console.print(table, justify="center")
console.save_svg("table.svg", title="iRead Blogs Table")
In the above code, we are creating a table of the latest five blogs posted on iRead. In the last line, we are using the save_svg()
.
Let us see the SVG image now.
Doesn't that look pretty cool? I found it amazing. Let me know what you think about it.
Display Progress Bar When Reading A File
This feature has been contributed by Martin Larralde who often has to process large datasets where you might want to see a progress bar. Suppose you're reading a JSON file, it was not easy to display a progress bar showing the update.
Martin's solution was to wrap file-like objects that read and seek to update the progress bar accordingly. That way when you pass a file-like object to a third-party API, you will get a progress bar that tracks how much of the file has been read.
Let us have an example JSON file with the following content.
[
{
"_id": "624b17b884324179f580f2c9",
"index": 0,
"guid": "4384af08-9f65-4b8c-bb34-daccfb45dcb2",
"isActive": true,
"balance": "$1,244.99",
"picture": "http://placehold.it/32x32",
"age": 37,
"eyeColor": "blue",
"name": "Vickie Flynn",
"gender": "female",
"company": "ZIDOX",
"email": "vickieflynn@zidox.com",
"phone": "+1 (879) 416-3606",
"address": "636 Newkirk Placez, Sexton, Missouri, 1532",
"about": "Fugiat aute aliquip esse veniam ex qui reprehenderit excepteur incididunt ad Lorem voluptate. Duis irure laborum qui ex ipsum adipisicing minim. Magna anim tempor tempor Lorem commodo ex enim irure quis est reprehenderit incididunt. Laboris labore do nulla do laboris pariatur id aute elit ea pariatur.\r\n",
"registered": "2014-05-04T04:15:21 -06:-30",
"latitude": -60.538619,
"longitude": 25.245925,
"tags": [
"esse",
"anim",
"occaecat",
"ipsum",
"exercitation",
"amet",
"ipsum"
],
"friends": [
{
"id": 0,
"name": "Christian Weaver"
},
{
"id": 1,
"name": "Terry Stanley"
},
{
"id": 2,
"name": "Cox Kirby"
}
],
"greeting": "Hello, Vickie Flynn! You have 2 unread messages.",
"favoriteFruit": "apple"
}
]
Let us see how we can read this JSON file.
import json
import rich.progress
with rich.progress.open("data.json") as file_handler:
data = json.load(file_handler)
Output:
Now adding a progress bar can be as simple as replacing open
with rich.progress.open
.
Conclusion
These were the latest updates to the Rich library. I loved them, what about you?
You can learn more about Rich here.