Rich is a Python library for 'rich' text and beautiful formatting in the terminal. It allows developers to add color, style, tables, progress bars, markdown, syntax highlighting, and more to command-line applications, making them much more engaging and user-friendly than plain text output. Rich aims to bring modern, sophisticated terminal user interfaces within reach for Python developers.
Key features of Rich include:
- Colored and Styled Text: Easily add foreground and background colors, bold, italic, underline, strikethrough, and other styles to text.
- Tables: Create highly customizable and visually appealing tables with borders, alignment, and styling.
- Progress Bars: Display dynamic and informative progress bars for long-running operations, including multiple bars and live updates.
- Status Messages: Show a spinning indicator with a status message for background tasks.
- Syntax Highlighting: Render code snippets with syntax highlighting for various programming languages.
- Markdown Rendering: Convert Markdown text into richly formatted terminal output.
- Logging Handler: A `RichHandler` for the Python `logging` module that makes log messages attractive and readable.
- Live Displays: Manage complex terminal layouts that update dynamically, such as dashboards or multiple progress bars.
- Emoji Support: Easily embed emojis in output.
- Console API: A powerful `Console` object that acts as an enhanced `print` function with styling and rendering capabilities.
Rich is an excellent tool for enhancing developer tools, CLI applications, scripts, and even debug output, significantly improving readability and the overall user experience in the terminal.
Example Code
from rich.console import Console
from rich.table import Table
from rich.progress import track
from rich.syntax import Syntax
from rich.panel import Panel
import time
console = Console()
1. Simple styled text
console.print("[bold red]Hello[/bold red] [green]Rich[/green] [underline blue]World![/underline blue]")
console.print("This is a message with [italic magenta]different[/italic magenta] [bold yellow on blue]styles[/bold yellow on blue] and [strike]effects[/strike].")
2. Creating a table
table = Table(title="[bold cyan]My Rich Table[/bold cyan]")
table.add_column("[bold green]Header 1[/bold green]", style="dim", width=12)
table.add_column("[bold magenta]Header 2[/bold magenta]", justify="right")
table.add_column("[bold yellow]Header 3[/bold yellow]")
table.add_row("Row 1 Col 1", "Row 1 Col 2", "Row 1 Col 3")
table.add_row("Row 2 Col 1", "[bold]Important[/bold]", "[red]Error![/red]")
table.add_row("Row 3 Col 1", "Another value", "Last one")
console.print(table)
3. Progress bar
console.print("\n[bold]Demonstrating a Progress Bar:[/bold]")
for i in track(range(100), description="[green]Processing data...[/green]"):
time.sleep(0.02) Simulate some work
console.print("[green]Processing complete![/green]")
4. Syntax highlighting
console.print("\n[bold]Python Code Snippet:[/bold]")
code = """
def factorial(n):
if n == 0:
return 1
else:
return n - factorial(n-1)
print(factorial(5))
"""
syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
console.print(syntax)
5. Using Panel
console.print(Panel("[bold blue]This is content inside a [green]panel[/green].\nIt can hold multiple lines of text.[/bold blue]", title="[red]A Nice Panel[/red]", border_style="yellow"))








Rich