django-rich

2.2.0 · active · verified Thu Apr 16

django-rich is a Python library that provides extensions for integrating the Rich library with Django projects. It enhances the Django development experience by enabling Rich's pretty-printing in the Django shell, providing Rich-powered tracebacks for the test runner, and offering a `RichCommand` base class for creating visually appealing management commands. The library is currently at version 2.2.0 and receives regular updates to support the latest Django and Python versions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a custom Django management command that leverages `django-rich`'s `RichCommand` base class. This allows you to use `Rich`'s console, status indicators, and styled output directly within your command, inheriting Django's colorization flags. Save this as `your_app/management/commands/mycommand.py` and run `python manage.py mycommand`.

import time
from django_rich.management import RichCommand

class Command(RichCommand):
    help = "A sample management command using django-rich."

    def handle(self, *args, **options):
        self.console.print("[bold blue]Starting a complex operation:[/bold blue]")

        with self.console.status("Processing items...") as status:
            for i in range(1, 11):
                status.update(f"[yellow]Processing item {i} of 10...[/yellow]")
                time.sleep(1)  # Simulate work
                self.console.log(f"[green]Item {i} processed.[/green]")

        self.console.print("[bold green]Operation complete![/bold green]")

view raw JSON →