rich 가 풍부한 텍스트 + 터미널 아름다운 포맷팅용 Python 라이브러리. 색, 테이블, progress bar, 문법 강조 코드, 포맷된 traceback. 한 import 로 스크립트에 떨어짐, 출력이 TTY 인지 자동 검출 (아닐 때 우아하게 degrade).
rich.print — 업그레이드
한 줄 — from rich import print 가 빌트인 가림. 이제 print({"a": 1, "b": [1, 2, 3]}) 가 색깔로 자동 pretty-print. print("[bold red]hello[/bold red]") 가 BBCode 같은 markup 사용. 출력 만드는 모든 스크립트의 빠른 승리.
테이블과 progress bar
border, alignment, 스타일 가진 실제 테이블엔 rich.table.Table. progress bar 보여주려고 iterable 감싸려면 rich.progress.track. 더 풍부한 다중 task progress 엔 rich.progress.Progress. 디폴트가 박스 밖에서 전문가스럽게.
rich traceback
from rich.traceback import install; install() — rich 를 디폴트 traceback 핸들러로 install. crash 가 이제 컨텍스트의 local 변수 표시 + 색깔, 문법 강조 traceback 생산. 평범한 Python traceback 보다 훨씬 빨리 읽힘. cwkPippa 가 사용.
Pythonic Way: 출력 읽는 사람 in-the-loop 인 모든 스크립트에 rich 가 무료 업그레이드. install() + from rich import print 두 줄이 모든 거 더 읽기 좋게. typer 가 rich 자동 통합, argparse + rich 도 ceremony 없이 작동.
Code
rich.print — drop-in 업그레이드·python
# pip install rich
from rich import print
# 복잡 타입 자동 pretty
print({"name": "alice", "items": [1, 2, 3], "active": True})
# {'name': 'alice', 'items': [1, 2, 3], 'active': True} — 색깔
# Markup 같은 styling
print("[bold red]error:[/bold red] something failed")
print("[green]ok[/green] all good")
print("[blue underline]https://example.com[/blue underline]")
from rich.progress import track
import time
# 어떤 iterable 도 감쌈
for item in track([1, 2, 3, 4, 5], description="처리 중..."):
time.sleep(0.5)
# ETA 가진 애니메이션 progress bar
# 더 제어
from rich.progress import Progress
with Progress() as progress:
task = progress.add_task("[red]다운로드 중...", total=100)
for _ in range(100):
time.sleep(0.01)
progress.update(task, advance=1)
rich traceback — 한 번 install·python
from rich.traceback import install
install(show_locals=True)
# 이제 잡히지 않은 모든 예외가 출력에 포함:
# - 문법 강조 소스 코드
# - 문제 줄 강조
# - 컨텍스트의 local 변수
def divide(a, b):
return a / b
# divide(10, 0)
# Traceback (most recent call last):
# File "...", line 8, in <module>
# divide(10, 0)
# File "...", line 6, in divide
# return a / b
# ZeroDivisionError: division by zero
#
# 근데 색깔 + local 변수 a=10, b=0 표시
작은 스크립트 — (a) 상단에 rich traceback install, (b) rich.print 로 색깔 환영 메시지 + dict 출력, (c) 3 컬럼 stock 데이터 + 5 row 의 Table 빌드, (d) rich.progress.track 으로 sleep 가진 range(20) 반복 감싸. 실행. 그 다음 일부러 ZeroDivisionError raise + rich traceback 관찰.
Progress
Progress is local-only — sign in to sync across devices.