rich/examples/columns.py

20 lines
560 B
Python
Raw Normal View History

2020-05-31 11:56:25 +00:00
import json
from urllib.request import urlopen
2020-05-31 14:22:23 +00:00
from rich import print
from rich.columns import Columns
from rich.panel import Panel
2020-05-31 14:07:37 +00:00
def get_content(user):
2020-05-31 14:32:27 +00:00
"""Extract text from user dict."""
2020-05-31 14:07:37 +00:00
country = user["location"]["country"]
name = f"{user['name']['first']} {user['name']['last']}"
return f"[b]{name}[/b]\n[yellow]{country}"
2020-05-31 14:32:27 +00:00
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"]
print(users)
2020-05-31 14:22:23 +00:00
user_renderables = [Panel(get_content(user), expand=False) for user in users]
2020-05-31 11:56:25 +00:00
print(Columns(user_renderables))