Skip to main content

Extensions for using Rich with Django.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/django-rich/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/django-rich.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Extensions for using Rich with Django.


Work smarter and faster with my book Boost Your Django DX which covers many ways to improve your development experience. I wrote django-rich whilst working on the book!


Requirements

Python 3.9 to 3.14 supported.

Django 4.2 to 6.0 supported.

Installation

  1. Install with pip:

    python -m pip install django-rich

None of django-rich’s features are activated by default. Follow the documentation below to use them.

Reference

shell command integration

django-rich has an extended version of Django’s built-in shell command that does two things:

  1. It enables Rich’s pretty-printing.

  2. On Django 5.2+, it adds automatic imports of these functions from Rich:

To activate this feature, add django_rich to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...,
    "django_rich",
    ...,
]

This feature only affects the Python and bypthon interpreters, not IPython. For IPython support, see the Rich documentation.

django_rich.management.RichCommand

A subclass of Django’s BaseCommand class that sets its self.console to a Rich Console. The Console uses the command’s stdout argument, which defaults to sys.stdout. Colourization is enabled or disabled according to Django’s --no-color and --force-color flags.

Use the features of self.console as you like:

from time import sleep

from django_rich.management import RichCommand


class Command(RichCommand):
    def handle(self, *args, **options):
        self.console.print("[bold blue]Frobnicating widgets:[/bold blue]")

        with self.console.status("Starting...") as status:
            for i in range(1, 11):
                status.update(f"Widget {i}...")
                sleep(1)
                self.console.log(f"Widget {i} frobnicated.")

You can customize the construction of the Console by overriding the make_rich_console() method. This takes some keyword arguments and passes them to the Console constructor. For example, to disable the on-by-default markup and highlighting flags, add them in a call to super():

from functools import partial

from django_rich.management import RichCommand
from rich.console import Console


class Command(RichCommand):
    def make_rich_console(self, **kwargs):
        return super().make_rich_console(**kwargs, markup=False, highlight=False)

    def handle(self, *args, **options): ...

django_rich.test.RichRunner

A subclass of Django’s DiscoverRunner with colourized outputs and nice traceback rendering.

https://raw.githubusercontent.com/adamchainz/django-rich/main/img/RichRunner.png

To use this class, point your TEST_RUNNER setting to it:

TEST_RUNNER = "django_rich.test.RichRunner"

You can also use it as a base for further customization. Since only output is modified, it should combine well with other classes.

The test runner provides the following features:

  • Output is colourized wherever possible. This includes Rich’s default highlighting which will format numbers, quoted strings, URL’s, and more.

  • Failures and errors use Rich’s traceback rendering. This displays the source code and local values per frame. Each frame also shows the filename and line number, and on many terminals you can click the link to jump to the file at that position.

  • Output is also colourized when using the --debug-sql and --pdb flags.

  • All other flags from Django’s DiscoverRunner continue to work in the normal way.

Output Width on CI

When tests run on your CI system, you might find the output a bit narrow for showing tracebacks correctly. This is because Rich tries to autodetect the terminal dimensions, and if that fails, it will default to 80 characters wide. You can override this default with the COLUMNS environment variable (as per Python’s shutil.get_terminal_size() function):

$ COLUMNS=120 ./manage.py test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_rich-2.2.0.tar.gz (62.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rich-2.2.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file django_rich-2.2.0.tar.gz.

File metadata

  • Download URL: django_rich-2.2.0.tar.gz
  • Upload date:
  • Size: 62.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_rich-2.2.0.tar.gz
Algorithm Hash digest
SHA256 ecec7842d040024ed8a225699388535e46b87277550c33f46193b52cece2f780
MD5 57750fe082ec2dc316403c21f1f5f4a1
BLAKE2b-256 a667e307a5fef657e7992468f567b521534c52e01bdda5a1ae5b12de679a670f

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rich-2.2.0.tar.gz:

Publisher: main.yml on adamchainz/django-rich

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_rich-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_rich-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_rich-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0d2c916bd9750b6e9beb57407aef5e836c8705d7dbe9e4fd4725f7bbe41c407
MD5 9634607536c2dac875e816e6b700f29d
BLAKE2b-256 27ed23fa669493d78cd67e7f6734fa380f8690f2b4d75b4f72fd645a52c3b32a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_rich-2.2.0-py3-none-any.whl:

Publisher: main.yml on adamchainz/django-rich

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page