🖥️
실습 프로젝트 (Practice) · ★★★★
표·진행바·컬러
Lv4 · TUI 앱 (rich/textual)
rich 라이브러리로 표/패널/컬러를 사용한 풍부한 콘솔 UI. 미설치 시 plain 모드로 폴백하는 안전 패턴까지.
richtextualTUI
소요 시간
⏱ 5~8시간
난이도
📊 고급 응용
선수 조건
🎯 고급 1주차 + 외부 라이브러리
결과물
라이브러리 의존성과 graceful fallback
이 강의에서 배우는 것
- 1rich 의 Table, Panel 사용
- 2ImportError 를 graceful 하게 처리
- 3설치: pip install rich
프로젝트 개요
- rich 또는 textual 사용
- 예: To-Do 또는 가계부를 TUI 로 재작성
- 미설치 시 plain 모드 폴백
도전 과제
- textual 풀스크린 + 키보드 단축키
- Live 로 실시간 갱신
설치
bash
pip install rich
# 또는
pip install textual채점 체크리스트
- rich 사용
- ImportError 폴백
- 표 출력
💻 예제 (examples)
실제로 실행해 결과를 확인할 수 있는 예제 코드입니다.
solution.py— rich 표 + 폴백
CODE
"""TUI To-Do (rich) — pip install rich"""
try:
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
HAS_RICH = True
except ImportError:
HAS_RICH = False
todos = [
{"id": 1, "task": "우유 사기", "priority": "보통", "done": False},
{"id": 2, "task": "책 반납", "priority": "높음", "done": True},
{"id": 3, "task": "운동", "priority": "낮음", "done": False},
]
PRIORITY_STYLE = {"높음": "red", "보통": "yellow", "낮음": "green"}
def render_rich():
console = Console()
table = Table(title="📋 My To-Do", show_lines=True, header_style="bold cyan")
table.add_column("번호", justify="right", style="cyan")
table.add_column("상태", justify="center")
table.add_column("우선순위", justify="center")
table.add_column("할 일")
for t in todos:
mark = "[green]✓ 완료[/]" if t["done"] else "[yellow]○ 진행[/]"
prio = f"[{PRIORITY_STYLE[t['priority']]}]{t['priority']}[/]"
task = f"[strike]{t['task']}[/]" if t["done"] else t["task"]
table.add_row(str(t["id"]), mark, prio, task)
console.print(table)
pending = sum(1 for t in todos if not t["done"])
console.print(Panel(f"미완료: [bold red]{pending}[/]개 / 전체 {len(todos)}개", style="dim"))
def render_plain():
print("📋 My To-Do (plain mode — pip install rich 권장)")
for t in todos:
mark = "[x]" if t["done"] else "[ ]"
print(f" {t['id']}. {mark}[{t['priority']}] {t['task']}")
def main():
if HAS_RICH:
render_rich()
else:
print("⚠️ rich 미설치. plain 모드로 출력.")
render_plain()
if __name__ == "__main__":
main()▶ 실행 결과
(rich 설치 시) 컬러 표 출력
(미설치 시)
⚠️ rich 미설치. plain 모드로 출력.
📋 My To-Do (plain mode — pip install rich 권장)
1. [ ][보통] 우유 사기
2. [x][높음] 책 반납
3. [ ][낮음] 운동📝 과제 (exercises)
직접 풀어보고, 막힐 때 정답을 펼쳐 비교해보세요.
과제 1
프로젝트 구현
목표: 위 사양대로 직접 작성한다.
요구사항
- try/except ImportError
- rich.Table
- polo 폴백
💡 힌트
table.add_column / table.add_row
입출력 예시
📋 My To-Do채점
- · 폴백 동작
- · rich 표
▶정답 코드 펼치기 / 접기
SOLUTION
# 위 solution.py 와 동일▶ 실행 결과
(예제 참고)