Fundamentals 11 min read

Master Rich: Transform Your Python Terminal Output with Colorful Formatting

This guide introduces the Rich Python library, showing how to install it, use its enhanced print function, create custom consoles, log output, render tables, progress bars, columns, markdown, syntax highlighting, emojis, and beautiful tracebacks for richer terminal applications.

Open Source Linux
Open Source Linux
Open Source Linux
Master Rich: Transform Your Python Terminal Output with Colorful Formatting

Rich is a Python library that brings rich text, colors, tables, progress bars, markdown rendering, syntax highlighting, logging, and more to terminal applications.

1. Compatibility

Rich works on Linux, macOS, and Windows, including the new Windows Terminal; classic Windows terminals support only eight colors.

2. Installation

Ensure Python and pip are installed, then run:

pip install rich

3. Print Function

Import the print function from Rich to produce colored and styled output:

from rich import print
print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())

The output displays colored text and highlights, offering a clear advantage over the built‑in print.

4. Custom Console

Create a Console object for advanced printing and logging:

from rich.console import Console
console = Console()

Use console.print similarly to print: console.print("Hello", "World!") Apply styles with the style argument, e.g., style="bold red".

5. Logging

The console provides a log() method that adds timestamps, file names, and line numbers, and can pretty‑print Python data structures:

console.log("Hello from", console, "!")
console.log(test_data, log_locals=True)

Rich can also format standard logging output with built‑in handlers.

6. Emojis

Insert emojis by wrapping their names in colons:

console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")

Result: 😃 🧛 💩 👍 🦝

7. Tables

Rich’s Table class creates flexible, auto‑wrapping tables with custom borders and alignment:

from rich.console import Console
from rich.table import Column, Table
console = Console()

table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", style="dim", width=12)
table.add_column("Title")
table.add_column("Production Budget", justify="right")
table.add_column("Box Office", justify="right")

table.add_row("Dec 15, 2017", "Star Wars Ep. VIII: The Last Jedi", "$262,000,000", "[bold]$1,332,539,889[/bold]")
console.print(table)
Table example
Table example

8. Progress Bars

Render one or multiple non‑flashing progress bars to track long‑running tasks:

from rich.progress import track
for step in track(range(100)):
    do_step(step)
Progress bar example
Progress bar example

9. Columns

Display data in evenly spaced columns using Columns:

from rich.columns import Columns
print(Columns(["item1", "item2", "item3"]))
Columns example
Columns example

10. Markdown Rendering

Render Markdown files directly in the terminal:

from rich.console import Console
from rich.markdown import Markdown
console = Console()
with open("README.md") as readme:
    markdown = Markdown(readme.read())
console.print(markdown)
Markdown rendering example
Markdown rendering example

11. Syntax Highlighting

Use Syntax to highlight source code with Pygments themes:

from rich.console import Console
from rich.syntax import Syntax
my_code = '''def hello():
    print("Hello World")''' 
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
Console().print(syntax)
Syntax highlighting example
Syntax highlighting example

12. Traceback Rendering

Rich can replace the default Python traceback with a more readable, colored version:

import sys
from rich.traceback import install
install()
# Trigger an exception to see the rich traceback
Rich traceback example
Rich traceback example

The article concludes with a reminder to follow the source for more Python practical tutorials.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIloggingrich
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.