/
Depends on
- dh-python (dh-sequence-python3, pybuild-plugin-pyproject)
- pytest (python3-pytest)
- python-coverage (python3-coverage)
- python-django (python3-django)
- python3-defaults (python3-all)
- rich (python3-rich)
- setuptools (python3-setuptools)
- debhelper-compat, python3-pytest-django
README (full)
django-rich
:target: https://github.com/adamchainz/django-rich/actions?workflow=CI
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:
print_json()____ https://rich.readthedocs.io/en/stable/console.html#printing-json
To activate this feature, add django_rich to your INSTALLED_APPS setting:
__ https://docs.djangoproject.com/en/stable/ref/django-admin/#shell
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.
__ https://rich.readthedocs.io/en/stable/console.html
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://docs.djangoproject.com/en/stable/topics/testing/advanced/#defining-a-test-runner
To use this class, point your TEST_RUNNER__ setting to it:
__ https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-TEST_RUNNER
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-sqland--pdbflags.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__):
__ https://docs.python.org/3/library/shutil.html#shutil.get_terminal_size
$ COLUMNS=120 ./manage.py test
| dir | .github/ |
| dir | debian/ |
| dir | img/ |
| dir | src/ |
| dir | tests/ |
| file | .editorconfig |
| file | .gitignore |
| file | .pre-commit-config.yaml |
| file | .typos.toml |
| file | CHANGELOG.rst |
| file | HISTORY.rst |
| file | LICENSE |
| file | MANIFEST.in |
| file | README.rst |
| file | pyproject.toml |
| file | tox.ini |
| file | uv.lock |