본문 바로가기
C.W.K.
Stream
Lesson 04 of 04 · published

Rich — 터미널 정보를 읽기 좋게 보여주기

~15 min · rich, terminal, color, progress

Level 0호기심
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete

Rich는 터미널의 색·글꼴·구조를 Python 객체로 다뤄. rich.print는 기본 print처럼 쓰면서 마크업과 딕셔너리 같은 값을 읽기 좋게 그리고, Console은 정렬·폭·표준 오류·기록을 세밀하게 제어해.

Table은 열과 행을 의미 단위로 맞추고, progress.track은 반복 작업의 진행률을 보여줘. 스피너·패널·구문 강조·마크다운도 있지만 장식보다 정보 계층과 접근성이 먼저야. 터미널이 아닌 파이프·로그·지속 통합에서는 평범한 무색 출력으로 물러나고, 색이 없어도 의미가 남아야 해.

rich.traceback.install()은 잡히지 않은 예외를 문법 강조한 소스와 선택적 지역 변수로 보여줘. 예외를 잡는 기능이 아니라 기존 예외 추적 표시기를 바꾸는 거야. 전체 터미널 사용자 환경이 필요해지면 Rich 위의 Textual도 살펴볼 수 있어.

Code

rich.print로 값과 마크업 출력하기·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]")
Table로 열과 행 맞추기·python
from rich.console import Console
from rich.table import Table

console = Console()

table = Table(title="Stocks", show_lines=True)
table.add_column("Ticker", style="cyan", no_wrap=True)
table.add_column("Price", justify="right", style="green")
table.add_column("Volume", justify="right")

table.add_row("AAPL", "$180.25", "45M")
table.add_row("NVDA", "$950.00", "32M")
table.add_row("MSFT", "$420.50", "28M")

console.print(table)
진행 막대로 반복 진행 보여주기·python
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 설치하기·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 표시

External links

Exercise

Rich traceback을 설치하고 색 있는 환영 문장과 딕셔너리를 출력해. 세 열·다섯 행의 주식 Table을 만들고 sleep이 든 range(20)을 progress.track으로 감싸. 마지막에 ZeroDivisionError를 일으켜 Rich traceback도 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.