Extensions for using Rich with Django.
Project description
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.13 supported.
Django 4.2 to 5.1 supported.
Installation
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 enables Rich’s pretty-printing. 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 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.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_rich-1.13.0.tar.gz
.
File metadata
- Download URL: django_rich-1.13.0.tar.gz
- Upload date:
- Size: 61.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a59a81ca3ba25202db7b6047aa322a8d5b866e1929f7fbea6059a3c8e4e420c |
|
MD5 | 328eea14183305f07298ca18ef886b1a |
|
BLAKE2b-256 | 03f0d3c450be9b319f9aba590decaf2858479e2399e78153c66045965bc9b951 |
File details
Details for the file django_rich-1.13.0-py3-none-any.whl
.
File metadata
- Download URL: django_rich-1.13.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9146dc0e0134bb970f156e8ba3015abd8982c35418fbab45004c561b521ee509 |
|
MD5 | 15767c44b576137769bdb8b357835dd0 |
|
BLAKE2b-256 | 6a67a81ebabc4c609045d884fc997087023c66a6b384d43bc0f039d17bc47fc3 |