Rich Python Library: Rich Text, Tables, Progress Bars, and Console Features
This article introduces the Python Rich library, detailing its cross‑platform compatibility, installation via pip, and features such as rich text printing, console logging, emoji support, tables, columns, markdown rendering, syntax highlighting, and progress bars with code examples.
1. Compatibility
The Rich library works on Linux, macOS, and Windows. True‑color and emoji support are available on newer Windows terminals, while classic terminals are limited to eight colors. Rich also integrates with Jupyter notebooks without extra configuration.
2. Installation
Install the library using pip or any other PyPI package manager:
<code>pip install rich</code>3. Printing Functionality
Import the Rich print function to replace the built‑in print and gain color, style, and automatic line‑wrapping:
<code>from rich import print
print("Hello, [bold magenta]World[/bold magenta]!", ":vampire:", locals())</code>4. Console Control
Create a Console object for advanced output control, similar to Python’s built‑in print :
<code>from rich.console import Console
console = Console()
console.print("Hello", "World!")</code>The console also provides a log() method that displays timestamps, file names, and line numbers, and highlights Python structures and repr strings.
<code>from rich.console import Console
console = Console()
console.log("Hello from", console, "!")
console.log(test_data, log_locals=True)</code>4.1 Console Logging
The log() method formats output similarly to print() but adds contextual information, making it useful for long‑running applications and debugging.
4.2 Logging Handlers
Rich can be used as a handler for Python’s standard logging module to add color and formatting to log records.
5. Emoji Support
Insert emojis by surrounding the name with colons:
<code>console.print(":smiley: :vampire: :pile_of_poo: :thumbs_up: :raccoon:")</code>6. Tables
Rich can render tables with Unicode box characters, offering many border styles, alignment options, and automatic column width adjustment:
<code>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)
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)</code>7. Progress Bars
Rich can render one or multiple non‑flashing progress bars to track long‑running tasks. Use the track function to iterate over a range while displaying progress:
<code>from rich.progress import track
for step in track(range(100)):
do_step(step)</code>8. Columns
Display items in evenly spaced columns, useful for listing directory contents:
<code>import os, sys
from rich import print
from rich.columns import Columns
directory = os.listdir(sys.argv[1])
print(Columns(directory))</code>9. Markdown Rendering
Render Markdown files directly in the terminal:
<code>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)</code>10. Syntax Highlighting
Use the Syntax class (built on Pygments) to display highlighted source code:
<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 = Console()
console.print(syntax)</code>11. Traceback Rendering
Rich can replace the default Python traceback printer with a more readable, color‑enhanced version, making debugging easier.
Original article link: https://www.escapelife.site/posts/d2a82698.html
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.