Back

Library Exploration: Rich

Optional — Extra Credit

divider

What This Is

Rich makes terminal output look good. Color, bold text, tables, boxes — all the things plain print() cannot do.

This one is deliberately placed right before Group Project 1. Your project is a terminal application, and the difference between a wall of gray text and something with a colored header and a real scoreboard is enormous for about ten minutes of work.

Nothing here changes how your program works. It changes how it looks. Get the program working first, then come back and make it pretty.


Installing It

Rich does not come with Python — unlike turtle or math, you have to install it:

Terminal
pip install rich

If that fails with a permissions error, the school machines may not let you install packages. Come and tell me rather than fighting it — there is a way around it, and it is not your problem to solve.


Work Through These

1. Color, the easy way

Import Rich's print instead of the built-in one and you can put style tags straight in your text. Everything else about print() works exactly as before.

Rich
from rich import print
print("[bold red]Danger![/bold red] The dragon noticed you.")
print("[green]You found 12 gold.[/green]")
print("[bold cyan]===== INVENTORY =====[/bold cyan]")

The tags come in pairs, like [green] and [/green] — the one with the slash turns the style back off. Try red, green, yellow, blue, magenta, cyan, and combinations like [bold underline red].


2. A Console object

The other way to do it: make a Console and pass a style separately. Useful when the same style gets reused a lot.

Rich
from rich.console import Console
console = Console()
console.print("Welcome to the shop!", style="bold magenta")
console.print("Sold out", style="dim")

3. A real table

This is the one worth learning. Remember the Kg to Lbs table, where lining up the columns by hand was the fiddly part? Rich does that for you.

Rich
from rich.console import Console
from rich.table import Table
console = Console()
table = Table(title="High Scores")
table.add_column("Player")
table.add_column("Score", justify="right")
table.add_row("Ana", "1200")
table.add_row("Ben", "980")
table.add_row("Cal", "870")
console.print(table)

Notice add_row takes strings, not numbers — so if your scores are integers, you will need to convert them on the way in. Which is casting, in the opposite direction from Activity 1.8.


4. Panels

Rich
from rich.console import Console
from rich.panel import Panel
console = Console()
console.print(Panel("You have entered the cave.", title="Chapter 2"))

Good for chapter headings in a story game, or for framing a menu.


Then Improve Something You Already Wrote

Do not start a new program for this. Go back to one of your finished code challenges and make it look better:

  • Give your Dungeon Crawler colored text for danger and treasure, and a panel for each chapter
  • Turn Heads or Tails' win/loss score into a real table
  • Color your Traffic Light output to match the actual light
  • Give your Playlist Builder a table with track numbers in one column and titles in the other

To submit: the before and after — your original file and the Rich version, so the difference is visible.

And keep it in mind for the group project. A terminal app with a colored header and a clean table reads as finished in a way that plain text does not.

Go Make It Look Good