Install django-rich
mainInstall django-rich using pip. Note that none of the features are activated by default; you must follow specific configuration steps to enable them.
python -m pip install django-richrepository·main·Indexed 41 days ago
https://github.com/adamchainz/django-richExtensions for using the Rich library within Django version 2.2.0. Provides pretty-printing in the Django shell, Rich-powered management commands via RichCommand, and colourized test outputs and tracebacks using RichRunner.
Install django-rich using pip. Note that none of the features are activated by default; you must follow specific configuration steps to enable them.
python -m pip install django-richTo enable Rich's pretty-printing and automatic imports of Rich functions in the Django shell command, add django_rich to your INSTALLED_APPS setting.
This feature provides automatic imports for the following functions on Django 5.2+:
inspect()print()print_json()pprint()Note: This only affects the standard Python and bpython interpreters. For IPython support, you must follow the official Rich documentation for IPython extensions.
INSTALLED_APPS = [
...,
"django_rich",
...,
]Use django_rich.test.RichRunner to get colourized test outputs and improved traceback rendering.
Features:
--debug-sql and --pdb flags.To activate, set TEST_RUNNER in your Django settings.
TEST_RUNNER = "django_rich.test.RichRunner"When running tests in CI, Rich might default to an 80-character width if it cannot detect the terminal size, which can make tracebacks look narrow. You can override this by setting the COLUMNS environment variable.
$ COLUMNS=120 ./manage.py testTo change how the Console is initialized in a RichCommand (for example, to disable markup or highlighting), override the make_rich_console method and pass arguments to super().
from django_rich.management import RichCommand
class Command(RichCommand):
def make_rich_console(self, **kwargs):
return super().make_rich_console(**kwargs, markup=False, highlight=False)
def handle(self, *args, **options): ...Use django_rich.management.RichCommand as a base class for your custom management commands to gain access to a Rich Console instance via self.console.
Key behaviors:
self.console uses the command's stdout (defaults to sys.stdout).--no-color and --force-color flags.Console by overriding make_rich_console(**kwargs).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.")To create Django management commands that support Rich formatting and colorized output, inherit from RichCommand instead of the standard BaseCommand.
RichCommand automatically sets up a rich.console.Console instance available as self.console. It also handles the logic for --no-color and --force-color flags, ensuring they are not used simultaneously.
When implementing your command, you can use self.console.print() to render rich text, tables, or other formatted content to the command's output.
from django_rich.management import RichCommand
class MyCustomCommand(RichCommand):
help = "A command with rich output"
def handle(self, *args, **options):
self.console.print("[bold green]Success![/bold green] This is rich output.")When running the enhanced shell, the following rich functions are automatically imported and available for use in your session:
rich.inspect: For inspecting objects.rich.print: For enhanced printing.rich.print_json: For pretty-printing JSON strings.rich.pretty.pprint: For pretty-printing Python objects.# These are available immediately upon entering the shell:
inspect(my_object)
print_json(my_json_string)
pprint(my_dict)The django-rich package provides an enhanced version of the standard Django shell management command. When using this command, rich is automatically installed to provide pretty-printed output for all standard console interactions.
Additionally, the shell is configured to automatically import several rich utilities, allowing you to use them immediately without manual imports.
python manage.py shell