Fundamentals 9 min read

Unlock Stunning Terminal Output with Python’s Rich Library – A Complete Guide

This article introduces the Rich Python library, detailing its cross‑platform compatibility, installation, rich printing, console control, logging, emojis, tables, progress bars, columns, markdown rendering, syntax highlighting, and enhanced tracebacks, all illustrated with code snippets and screenshots.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlock Stunning Terminal Output with Python’s Rich Library – A Complete Guide

Rich is a powerful Python library that provides rich text, formatting, emojis, tables, progress bars, markdown, syntax highlighting, and more for terminal output.

1. Compatibility

Rich works on Linux, macOS, and Windows. True‑color and emoji support works with newer Windows terminals, while classic Windows console is limited to 8 colors. Rich also integrates with Jupyter notebooks without extra configuration.

2. Installation

Install via pip or any PyPI package manager:

pip install rich

3. Printing

Import the rich print function and use it like the built‑in print, but with support for styling and emojis.

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

4. Console

For finer control, create a Console object. Its print method mirrors the built‑in print, automatically wrapping text to the terminal width.

from rich.console import Console
console = Console()
console.print("Hello", "World!")

4.1 Logging

The Console also provides a log() method similar to print() but adds timestamps, file, and line information. It can pretty‑print Python objects, and with log_locals=True it displays a table of local variables.

from rich.console import Console
console = Console()
console.log("Message", log_locals=True)

4.2 Log Handler

Rich includes a logging handler that formats and colors output from Python’s logging module.

5. Emojis

Wrap an emoji name in colons to render it in the console.

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

6. Tables

Rich can render tables with Unicode box characters, customizable borders, styles, and alignment.

from rich.console import Console
from rich.table import Table
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Date", style="dim", width=12)
# add more columns and rows …
console.print(table)

7. Progress Bars

Use track to wrap any iterable and display a non‑flashing progress bar.

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

8. Columns

Display a list of items in evenly spaced columns, similar to the Unix ls output.

from rich import print
from rich.columns import Columns
print(Columns(directory))

9. Markdown

Render markdown by creating a Markdown object and printing it.

from rich.console import Console
from rich.markdown import Markdown
console = Console()
markdown = Markdown(open("README.md").read())
console.print(markdown)

10. Syntax Highlighting

Rich uses Pygments to highlight code snippets.

from rich.console import Console
from rich.syntax import Syntax
syntax = Syntax(my_code, "python", theme="monokai", line_numbers=True)
Console().print(syntax)

11. Tracebacks

Rich can render beautiful tracebacks that are easier to read than the default Python tracebacks. Set Rich as the default traceback handler to improve error reporting.

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.

Pythonformattingprogress barconsolerichterminal
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.