Fundamentals 10 min read

Unlock Beautiful Terminal Output in Python with Rich

This tutorial introduces the Rich Python library, covering its cross‑platform compatibility, installation steps, enhanced print functions, custom console usage, logging, tables, progress bars, markdown rendering, syntax highlighting, and traceback formatting, all illustrated with clear code examples and screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlock Beautiful Terminal Output in Python with Rich

1. Rich Compatibility

Rich works on Linux, macOS, and Windows. It can be used with the new Windows Terminal, while the classic Windows console supports only eight colors.

Rich also integrates with Jupyter Notebook without additional configuration.

2. Installation

Make sure Python and pip are installed. Install Rich via pip install rich. For data‑analysis tasks you may prefer Anaconda, which bundles Python and pip, or use VSCode as your editor.

3. Rich Print Function

Import Rich's print function and use it like the built‑in print, but with colors and styles.

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

4. Custom Console Output

Create a Console object to gain more control over terminal output.

from rich.console import Console
console = Console()

Use console.print similarly to the built‑in print. console.print("Hello", "World!") Apply the style argument to customize colors and text attributes.

console.print("Hello", "World!", style="bold red")

5. Console Logging

The Console object provides a log() method that works like print() but also shows timestamps, file names, and line numbers.

Rich automatically highlights Python data structures and can display locals when log_locals=True is used.

from rich.console import Console
console = Console()
# Example data and logging
console.log("Hello from", console, "!")
console.log(test_data, log_locals=True)

6. Emoji Support

Insert emojis by wrapping their names with colons.

console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")
# Output: 😃 🧛 💩 👍 🦝

7. Tables

Rich can render richly formatted tables with borders, alignment, and automatic column width adjustment.

from rich.console import Console
from rich.table import Table, Column
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")
# Add rows …
console.print(table)

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)

9. Columnar Output

Display data in evenly spaced columns, useful for reproducing commands like ls.

import os, sys
from rich import print
from rich.columns import Columns
directory = os.listdir(sys.argv[1])
print(Columns(directory))

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)

11. Syntax Highlighting

Use the Syntax class (powered by Pygments) to display highlighted source code.

from rich.console import Console
from rich.syntax import Syntax
my_code = '''
def iter_first_last(values):
    ...
'''  # truncated for brevity
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
Console().print(syntax)

12. Traceback Formatting

Rich can render beautiful, more readable tracebacks than the default Python output.

Set Rich as the default traceback handler to automatically format all exceptions.

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.

Pythonformattingcodeconsolerichterminal
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.