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?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.

Requirements

Python 3.7 to 3.12 supported.

Django 3.2 to 4.2 supported.


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


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

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.

You can use self.console like so:

from django_rich.management import RichCommand


class Command(RichCommand):
    def handle(self, *args, **options):
        self.console.print("[bold red]Alert![/bold red]")

You can customize the construction of the Console by overriding the make_rich_console class attribute. This should be a callable that returns a Console, such as a functools.partial. For example, to disable the default-on markup and highlighting flags:

from functools import partial

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


class Command(RichCommand):
    make_rich_console = partial(Console, 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-1.6.0.tar.gz (61.8 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-1.6.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django_rich-1.6.0.tar.gz
  • Upload date:
  • Size: 61.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for django_rich-1.6.0.tar.gz
Algorithm Hash digest
SHA256 2eaa70b07b76d84d305699f1a522f9f4a026286e33f0928fc6716f45ed5e0f64
MD5 01a2da0e8f4e87fa64a835f9620c55c0
BLAKE2b-256 6082e0fb105d9f866228a5baa6f090bbffefb5bb461df5127665fc3d7a943149

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_rich-1.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for django_rich-1.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0f55575f6c45cbf70fc45d42512f595fc44873d94e7aa43acf3173fa72061b8c
MD5 30df5b8afdfec478bfb7e7f9f10ddfa5
BLAKE2b-256 283b09cb5eafb16c032af97ecfa1604fb772899b6e5c250a3c6fe07fb21ec56f

See more details on using hashes here.

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