Files
org-roamings/20220130153624-textual.org

3.0 KiB

textual

Introduction

Framework de création d'IHM inspiré par le developpement web moderne (CSS) basé sur Rich. Rich est une librairie Python permettant de :

  • Appliquer des styles à la sortie sur le terminal,
  • Afficher des tables et du contenu markdown,
  • Appliquer une coloration synthaxique.

Howto

Pour inspecter un objet (/rich.inspect method)

from rich import inspect

class Dummy:

    def __init__(self):
        self.a = 'A'
        self.b = 123

inspect(vars)

inspect(Dummy())
╭────────────────── <built-in function vars> ──────────────────╮
│ def vars(...)                                                │
│                                                              │
│ vars([object]) -> dictionary                                 │
│                                                              │
│ 29 attribute(s) not shown. Run inspect(inspect) for options. │
╰──────────────────────────────────────────────────────────────╯
╭────────── <class '__main__.Dummy'> ───────────╮
│ ╭───────────────────────────────────────────╮ │
│ │ <__main__.Dummy object at 0x7f55d6d2d7c0> │ │
│ ╰───────────────────────────────────────────╯ │
│                                               │
│ a = 'A'                                       │
│ b = 123                                       │
╰───────────────────────────────────────────────╯

Formatter les logs

import logging
from rich.logging import RichHandler

FORMAT = "%(message)s"
logging.basicConfig(
    level="NOTSET",
    format=FORMAT,
    datefmt="[%X]",
    handlers=[RichHandler(rich_tracebacks=True)])

log = logging.getLogger("rich")

log.info("Logging set up.")

def division(a, b):
    log.debug(f"Dividing {a} by {b}.")
    try:
        return a / b
    except ZeroDivisionError:
        log.exception("Oh noes!")
    
division(3, 2)
division(5, 0)