Skip to main content

Table renderer for dataclass using Rich

Project description

Richer

Table renderer for dataclass using Rich

Features

List Table

Display table from dataclasses

1_list_table.py

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 │
└─────────┴────────────┴─────────────────────┘

Column Manipulations

Display a table with custom columns

  • Justify
  • Sort
  • Hide/Show

1-1_column_manipulation.py

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 │
└─────────┴──────────────┘

Property Table

Display table from a dataclass

2_property_table.py

from richer.table import PropertyTable

console.print(PropertyTable(item[0]))
┌────────────┬─────────────────────┐
│ NAME       │ Vue                 │
├────────────┼─────────────────────┤
│ STAR COUNT │ 156,110             │
├────────────┼─────────────────────┤
│ START DATE │ 2013-07-29 00:00:00 │
└────────────┴─────────────────────┘

Inner Table

Display inner table from a nested dataclass

3_inner_table.py

@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  │
└────────────┴─────────────────────┘

Pagination a table

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.

4_pagination.py

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 │
└────┘

ANSI Escape Text

Display ANSI Escape Text

It is useful to use rich by redirecting stdout and stderr

5_ansi_escape_text.py

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

richer-0.1.6-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file richer-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: richer-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.56.2 CPython/3.8.5

File hashes

Hashes for richer-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1498ed7aeeaca6d5f446bf5ab2511322ba10a0a65921af2dc0f1c6b73c2bbc27
MD5 eaeaf8ce7368c7468a6ea9f7ec74b2ab
BLAKE2b-256 b21113681de0e08f14f20005589c70658c90c0ec19aebc7900dfb5126fec697e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page