| 1 |
""" |
| 2 |
This example shows how to display content in columns. |
| 3 |
|
| 4 |
The data is pulled from https://randomuser.me |
| 5 |
""" |
| 6 |
|
| 7 |
import json |
| 8 |
from urllib.request import urlopen |
| 9 |
|
| 10 |
from rich.console import Console |
| 11 |
from rich.columns import Columns |
| 12 |
from rich.panel import Panel |
| 13 |
|
| 14 |
|
| 15 |
def get_content(user): |
| 16 |
"""Extract text from user dict.""" |
| 17 |
country = user["location"]["country"] |
| 18 |
name = f"{user['name']['first']} {user['name']['last']}" |
| 19 |
return f"[b]{name}[/b]\n[yellow]{country}" |
| 20 |
|
| 21 |
|
| 22 |
console = Console() |
| 23 |
|
| 24 |
|
| 25 |
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"] |
| 26 |
console.print(users, overflow="ignore", crop=False) |
| 27 |
user_renderables = [Panel(get_content(user), expand=True) for user in users] |
| 28 |
console.print(Columns(user_renderables)) |