Git Product home page Git Product logo

django-rich's Introduction

django-rich

image

image

image

image

pre-commit

Extensions for using Rich with Django.

Requirements

Python 3.7 to 3.10 supported.

Django 3.2 to 4.0 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

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.

image

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

django-rich's People

Contributors

adamchainz avatar pre-commit-ci[bot] avatar smithdc1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.