Mini Project: Use a Library
Exit

Mini Project: Use a Library

Install and use the rich library to build colorful terminal output

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Using a third-party library

You installed rich earlier in this lesson. Now you will use it to build formatted terminal output. This is the workflow you will follow for every third-party package: install, import, use.

The rich library

rich makes terminal output look professional. Instead of plain print(), you can use:

  • Console — a replacement for print() with color and style support
  • Table — displays data in formatted tables
  • Panel — wraps text in a bordered box

from rich.console import Console
from rich.table import Table

console = Console()
console.print("Hello!", style="bold green")

Building a table

rich tables work in three steps:

  1. Create a Table object with a title
  2. Add columns with add_column()
  3. Add rows with add_row()

table = Table(title="My Data")
table.add_column("Name", style="cyan")
table.add_column("Value", style="green")
table.add_row("Python", "3.12")
console.print(table)

Your task

Build a project dependency viewer. Create a table that displays package information, then wrap it in styled output. This mimics what tools like pip list do, but with better formatting.

Instructions

Build a formatted dependency report using rich.

  1. Import Console from rich.console and Table from rich.table.
  2. Create a variable named console and assign it Console().
  3. Create a variable named packages and assign it the list [("numpy", "1.26.4", "Math"), ("pypdf", "4.1.0", "PDF"), ("rich", "13.7.0", "Terminal")].
  4. Create a variable named table and assign it Table(title="Project Dependencies").
  5. Call table.add_column("Package", style="cyan").
  6. Call table.add_column("Version", style="green").
  7. Call table.add_column("Purpose", style="yellow").
  8. Loop through packages with loop variable name, version, purpose. Inside the loop, call table.add_row(name, version, purpose).
  9. Call console.print(table).
  10. Call console.print() with f"Total: {len(packages)} packages" and style="bold".