Table renderer for dataclass using Rich
Display table from dataclasses
from rich.console import Console
from richer.table import ListTable
@dataclass
class Project:
name: str
star_count: int
start_date: datetime
items = [
Project('Vue', 156110, datetime.fromisoformat('2013-07-29T00:00:00')),
Project('React', 142857, datetime.fromisoformat('2013-05-25T00:00:00')),
Project('Angular', 57004, datetime.fromisoformat('2014-09-19T00:00:00'))
]
console = Console()
console.print(ListTable(items))
┌─────────┬────────────┬─────────────────────┐
│ NAME │ STAR COUNT │ START DATE │
├─────────┼────────────┼─────────────────────┤
│ Vue │ 156,110 │ 2013-07-29 00:00:00 │
│ React │ 142,857 │ 2013-05-25 00:00:00 │
│ Angular │ 57,004 │ 2014-09-19 00:00:00 │
└─────────┴────────────┴─────────────────────┘
Display a table with custom columns
- Justify
- Sort
- Hide/Show
from rich.console import Console
from richer.table import Column, ListTable
table = ListTable(
items,
columns=[
Column(name='star_count', order='asc', justify='right'),
Column(name='start_date', visible=False)
]
)
console = Console()
console.print(table)
┌─────────┬──────────────┐
│ NAME │ STAR COUNT ▲ │
├─────────┼──────────────┤
│ Angular │ 57,004 │
│ React │ 142,857 │
│ Vue │ 156,110 │
└─────────┴──────────────┘
Display table from a dataclass
from richer.table import PropertyTable
console.print(PropertyTable(item[0]))
┌────────────┬─────────────────────┐
│ NAME │ Vue │
├────────────┼─────────────────────┤
│ STAR COUNT │ 156,110 │
├────────────┼─────────────────────┤
│ START DATE │ 2013-07-29 00:00:00 │
└────────────┴─────────────────────┘
Display inner table from a nested dataclass
@dataclass
class Tag:
name: str
commit: str
@dataclass
class Project:
name: str
star_count: int
start_date: datetime
tags: List[Tag]
tags = [
Tag('v17.0.0', '89b6109'),
Tag('v16.0.0', '5c6ef40')
]
item = Project(
'React',
142857,
datetime.fromisoformat('2013-05-25T00:00:00'),
tags
)
console.print(PropertyTable(item))
┌────────────┬─────────────────────┐
│ NAME │ React │
├────────────┼─────────────────────┤
│ STAR COUNT │ 142,857 │
├────────────┼─────────────────────┤
│ START DATE │ 2013-05-25 00:00:00 │
├────────────┼─────────────────────┤
│ TAGS │ NAME │ COMMIT │
│ │ ─────────┼───────── │
│ │ v17.0.0 │ 89b6109 │
│ │ v16.0.0 │ 5c6ef40 │
└────────────┴─────────────────────┘
Paginate a table using curses
- Press 'q' key to exit from interactive console.
- Press 'right' key or 'page down' key to go to next page.
- Press 'left' key or 'page up' key to go to previous page.
from richer.console import InteractiveConsole
@dataclass
class Row:
id: int
items = [Row(r) for r in range(0, 100)]
console = InteractiveConsole(items)
console.print()
┌────┐
│ ID │
├────┤
│ 0 │
│ 1 │
│ 2 │
│ 3 │
│ 4 │
│ 5 │
│ 6 │
│ 7 │
│ 8 │
│ 9 │
│ 10 │
└────┘
Display ANSI Escape Text
It is useful to use rich by redirecting stdout and stderr
from richer.text import AnsiEscapeText
console = Console()
text = '\x1b[1;32mSuccess\x1b[0m\n\x1b[1;31mFailure\x1b[0m'
console.print(AnsiEscapeText(text))
Success
Failure