django-rich


Namedjango-rich JSON
Version 1.14.0 PyPI version JSON
download
home_pageNone
SummaryExtensions for using Rich with Django.
upload_time2025-02-06 22:22:19
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseNone
keywords django
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===========
django-rich
===========

.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/django-rich/main.yml.svg?branch=main&style=for-the-badge
   :target: https://github.com/adamchainz/django-rich/actions?workflow=CI

.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge
  :target: https://github.com/adamchainz/django-rich/actions?workflow=CI

.. image:: https://img.shields.io/pypi/v/django-rich.svg?style=for-the-badge
   :target: https://pypi.org/project/django-rich/

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge
   :target: https://github.com/psf/black

.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge
   :target: https://github.com/pre-commit/pre-commit
   :alt: pre-commit

Extensions for using `Rich <https://rich.readthedocs.io/>`__ with Django.

----

**Work smarter and faster** with my book `Boost Your Django DX <https://adamchainz.gumroad.com/l/byddx>`__ 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.2 supported.

Installation
------------

1. Install with **pip**:

   .. code-block:: sh

       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 <https://rich.readthedocs.io/en/stable/introduction.html?highlight=install#rich-in-the-repl>`__.
To activate this feature, add ``django_rich`` to your ``INSTALLED_APPS`` setting:

.. |shell command| replace:: ``shell`` command
__ https://docs.djangoproject.com/en/stable/ref/django-admin/#shell

   .. code-block:: python

       INSTALLED_APPS = [
           ...,
           "django_rich",
           ...,
       ]

This feature only affects the Python and bypthon interpreters, not IPython.
For IPython support, see `the Rich documentation <https://rich.readthedocs.io/en/stable/introduction.html#ipython-extension>`__.

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

.. |BaseCommand| replace:: ``BaseCommand``
__ https://docs.djangoproject.com/en/stable/howto/custom-management-commands/#django.core.management.BaseCommand

.. |Console| replace:: ``Console``
__ https://rich.readthedocs.io/en/stable/console.html

Use the features of ``self.console`` as you like:

.. code-block:: python

    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:

.. |functools.partial| replace:: ``functools.partial``
__ https://docs.python.org/3/library/functools.html#functools.partial

.. code-block:: python

    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://rich.readthedocs.io/en/stable/traceback.html>`__.

.. image:: https://raw.githubusercontent.com/adamchainz/django-rich/main/img/RichRunner.png

.. |DiscoverRunner| replace:: ``DiscoverRunner``
__ https://docs.djangoproject.com/en/stable/topics/testing/advanced/#defining-a-test-runner

To use this class, point your |TEST_RUNNER|__ setting to it:

.. |TEST_RUNNER| replace:: ``TEST_RUNNER``
__ https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-TEST_RUNNER

.. code-block:: python

    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 <https://rich.readthedocs.io/en/stable/highlighting.html>`__ which will format numbers, quoted strings, URL’s, and more.

* Failures and errors use Rich’s `traceback rendering <https://rich.readthedocs.io/en/stable/traceback.html>`__.
  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|__):

.. |shutil.get_terminal_size() function| replace:: ``shutil.get_terminal_size()`` function
__ https://docs.python.org/3/library/shutil.html#shutil.get_terminal_size

.. code-block:: console

    $ COLUMNS=120 ./manage.py test

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "django-rich",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "Django",
    "author": null,
    "author_email": "Adam Johnson <me@adamj.eu>",
    "download_url": "https://files.pythonhosted.org/packages/19/1e/db0d717a256934cdbeef02fe0fce88a1e0ef11b60a98125ee3dbf2fd3eae/django_rich-1.14.0.tar.gz",
    "platform": null,
    "description": "===========\ndjango-rich\n===========\n\n.. image:: https://img.shields.io/github/actions/workflow/status/adamchainz/django-rich/main.yml.svg?branch=main&style=for-the-badge\n   :target: https://github.com/adamchainz/django-rich/actions?workflow=CI\n\n.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge\n  :target: https://github.com/adamchainz/django-rich/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/django-rich.svg?style=for-the-badge\n   :target: https://pypi.org/project/django-rich/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n   :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge\n   :target: https://github.com/pre-commit/pre-commit\n   :alt: pre-commit\n\nExtensions for using `Rich <https://rich.readthedocs.io/>`__ with Django.\n\n----\n\n**Work smarter and faster** with my book `Boost Your Django DX <https://adamchainz.gumroad.com/l/byddx>`__ which covers many ways to improve your development experience.\nI wrote django-rich whilst working on the book!\n\n----\n\nRequirements\n------------\n\nPython 3.9 to 3.13 supported.\n\nDjango 4.2 to 5.2 supported.\n\nInstallation\n------------\n\n1. Install with **pip**:\n\n   .. code-block:: sh\n\n       python -m pip install django-rich\n\nNone of django-rich\u2019s features are activated by default.\nFollow the documentation below to use them.\n\nReference\n---------\n\n``shell`` command integration\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\ndjango-rich has an extended version of Django\u2019s built-in |shell command|__ that enables `Rich\u2019s pretty-printing <https://rich.readthedocs.io/en/stable/introduction.html?highlight=install#rich-in-the-repl>`__.\nTo activate this feature, add ``django_rich`` to your ``INSTALLED_APPS`` setting:\n\n.. |shell command| replace:: ``shell`` command\n__ https://docs.djangoproject.com/en/stable/ref/django-admin/#shell\n\n   .. code-block:: python\n\n       INSTALLED_APPS = [\n           ...,\n           \"django_rich\",\n           ...,\n       ]\n\nThis feature only affects the Python and bypthon interpreters, not IPython.\nFor IPython support, see `the Rich documentation <https://rich.readthedocs.io/en/stable/introduction.html#ipython-extension>`__.\n\n``django_rich.management.RichCommand``\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nA subclass of Django\u2019s |BaseCommand|__ class that sets its ``self.console`` to a Rich |Console|__.\nThe ``Console`` uses the command\u2019s ``stdout`` argument, which defaults to ``sys.stdout``.\nColourization is enabled or disabled according to Django\u2019s ``--no-color`` and ``--force-color`` flags.\n\n.. |BaseCommand| replace:: ``BaseCommand``\n__ https://docs.djangoproject.com/en/stable/howto/custom-management-commands/#django.core.management.BaseCommand\n\n.. |Console| replace:: ``Console``\n__ https://rich.readthedocs.io/en/stable/console.html\n\nUse the features of ``self.console`` as you like:\n\n.. code-block:: python\n\n    from time import sleep\n\n    from django_rich.management import RichCommand\n\n\n    class Command(RichCommand):\n        def handle(self, *args, **options):\n            self.console.print(\"[bold blue]Frobnicating widgets:[/bold blue]\")\n\n            with self.console.status(\"Starting...\") as status:\n                for i in range(1, 11):\n                    status.update(f\"Widget {i}...\")\n                    sleep(1)\n                    self.console.log(f\"Widget {i} frobnicated.\")\n\nYou can customize the construction of the ``Console`` by overriding the ``make_rich_console`` class attribute.\nThis should be a callable that returns a ``Console``, such as a |functools.partial|__.\nFor example, to disable the default-on ``markup`` and ``highlighting`` flags:\n\n.. |functools.partial| replace:: ``functools.partial``\n__ https://docs.python.org/3/library/functools.html#functools.partial\n\n.. code-block:: python\n\n    from functools import partial\n\n    from django_rich.management import RichCommand\n    from rich.console import Console\n\n\n    class Command(RichCommand):\n        make_rich_console = partial(Console, markup=False, highlight=False)\n\n        def handle(self, *args, **options): ...\n\n``django_rich.test.RichRunner``\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nA subclass of Django's |DiscoverRunner|__ with colourized outputs and `nice traceback rendering <https://rich.readthedocs.io/en/stable/traceback.html>`__.\n\n.. image:: https://raw.githubusercontent.com/adamchainz/django-rich/main/img/RichRunner.png\n\n.. |DiscoverRunner| replace:: ``DiscoverRunner``\n__ https://docs.djangoproject.com/en/stable/topics/testing/advanced/#defining-a-test-runner\n\nTo use this class, point your |TEST_RUNNER|__ setting to it:\n\n.. |TEST_RUNNER| replace:: ``TEST_RUNNER``\n__ https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-TEST_RUNNER\n\n.. code-block:: python\n\n    TEST_RUNNER = \"django_rich.test.RichRunner\"\n\nYou can also use it as a base for further customization.\nSince only output is modified, it should combine well with other classes.\n\nThe test runner provides the following features:\n\n* Output is colourized wherever possible.\n  This includes Rich\u2019s default `highlighting <https://rich.readthedocs.io/en/stable/highlighting.html>`__ which will format numbers, quoted strings, URL\u2019s, and more.\n\n* Failures and errors use Rich\u2019s `traceback rendering <https://rich.readthedocs.io/en/stable/traceback.html>`__.\n  This displays the source code and local values per frame.\n  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.\n\n* Output is also colourized when using the ``--debug-sql`` and ``--pdb`` flags.\n\n* All other flags from Django's DiscoverRunner continue to work in the normal way.\n\nOutput Width on CI\n~~~~~~~~~~~~~~~~~~\n\nWhen tests run on your CI system, you might find the output a bit narrow for showing tracebacks correctly.\nThis is because Rich tries to autodetect the terminal dimensions, and if that fails, it will default to 80 characters wide.\nYou can override this default with the ``COLUMNS`` environment variable (as per Python\u2019s |shutil.get_terminal_size() function|__):\n\n.. |shutil.get_terminal_size() function| replace:: ``shutil.get_terminal_size()`` function\n__ https://docs.python.org/3/library/shutil.html#shutil.get_terminal_size\n\n.. code-block:: console\n\n    $ COLUMNS=120 ./manage.py test\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Extensions for using Rich with Django.",
    "version": "1.14.0",
    "project_urls": {
        "Changelog": "https://github.com/adamchainz/django-rich/blob/main/CHANGELOG.rst",
        "Funding": "https://adamj.eu/books/",
        "Repository": "https://github.com/adamchainz/django-rich"
    },
    "split_keywords": [
        "django"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0048d4ab93077c23a740c08520b3a8941895243d567f2280d61d2a7e8073887c",
                "md5": "466879b0add1ba6f22c3412f0d31bb0e",
                "sha256": "f45137207b56361634eeb8bd9e439eec2f7d88a79ed1289f09647b66650763f9"
            },
            "downloads": -1,
            "filename": "django_rich-1.14.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "466879b0add1ba6f22c3412f0d31bb0e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 8857,
            "upload_time": "2025-02-06T22:22:17",
            "upload_time_iso_8601": "2025-02-06T22:22:17.240747Z",
            "url": "https://files.pythonhosted.org/packages/00/48/d4ab93077c23a740c08520b3a8941895243d567f2280d61d2a7e8073887c/django_rich-1.14.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "191edb0d717a256934cdbeef02fe0fce88a1e0ef11b60a98125ee3dbf2fd3eae",
                "md5": "43badff723d5e1076415cfb64c4e3330",
                "sha256": "737b3093a9ba993a40cefc5fb0d7bb12767adb78c9944e6d3107805caca3dcd7"
            },
            "downloads": -1,
            "filename": "django_rich-1.14.0.tar.gz",
            "has_sig": false,
            "md5_digest": "43badff723d5e1076415cfb64c4e3330",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 61077,
            "upload_time": "2025-02-06T22:22:19",
            "upload_time_iso_8601": "2025-02-06T22:22:19.421590Z",
            "url": "https://files.pythonhosted.org/packages/19/1e/db0d717a256934cdbeef02fe0fce88a1e0ef11b60a98125ee3dbf2fd3eae/django_rich-1.14.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-06 22:22:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adamchainz",
    "github_project": "django-rich",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "django-rich"
}
        
Elapsed time: 0.50862s