2020-06-01 15:08:06 +00:00
|
|
|
"""
|
|
|
|
This example shows how to display content in columns.
|
|
|
|
|
|
|
|
The data is pulled from https://randomuser.me
|
|
|
|
"""
|
|
|
|
|
2020-05-31 11:56:25 +00:00
|
|
|
import json
|
|
|
|
from urllib.request import urlopen
|
|
|
|
|
2020-08-12 20:34:24 +00:00
|
|
|
from rich.console import Console
|
2020-05-31 14:22:23 +00:00
|
|
|
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-08-20 12:54:39 +00:00
|
|
|
|
2020-08-12 20:34:24 +00:00
|
|
|
console = Console()
|
|
|
|
|
2020-05-31 14:07:37 +00:00
|
|
|
|
2020-05-31 14:32:27 +00:00
|
|
|
users = json.loads(urlopen("https://randomuser.me/api/?results=30").read())["results"]
|
2020-08-12 20:34:24 +00:00
|
|
|
console.print(users, overflow="ignore", crop=False)
|
2020-06-01 18:18:42 +00:00
|
|
|
user_renderables = [Panel(get_content(user), expand=True) for user in users]
|
2020-08-12 20:34:24 +00:00
|
|
|
console.print(Columns(user_renderables))
|