django-rich


Namedjango-rich JSON
Version 1.8.0 PyPI version JSON
download
home_pagehttps://github.com/adamchainz/django-rich
SummaryExtensions for using Rich with Django.
upload_time2023-10-11 09:36:20
maintainer
docs_urlNone
authorAdam Johnson
requires_python>=3.8
licenseMIT
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?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.

Requirements
------------

Python 3.8 to 3.12 supported.

Django 3.2 to 5.0 supported.

----

**Want to work smarter and faster?**
Check out 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!

----

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

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

You can use ``self.console`` like so:

.. code-block:: python

    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:

.. |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": "https://github.com/adamchainz/django-rich",
    "name": "django-rich",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "Django",
    "author": "Adam Johnson",
    "author_email": "me@adamj.eu",
    "download_url": "https://files.pythonhosted.org/packages/5b/42/39c6525d465dbb266547e788622ca35b370c16756e7db9b1cefae7665d11/django_rich-1.8.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?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\nRequirements\n------------\n\nPython 3.8 to 3.12 supported.\n\nDjango 3.2 to 5.0 supported.\n\n----\n\n**Want to work smarter and faster?**\nCheck out 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\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``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\nYou can use ``self.console`` like so:\n\n.. code-block:: python\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 red]Alert![/bold red]\")\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\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": "MIT",
    "summary": "Extensions for using Rich with Django.",
    "version": "1.8.0",
    "project_urls": {
        "Changelog": "https://github.com/adamchainz/django-rich/blob/main/CHANGELOG.rst",
        "Homepage": "https://github.com/adamchainz/django-rich",
        "Mastodon": "https://fosstodon.org/@adamchainz",
        "Twitter": "https://twitter.com/adamchainz"
    },
    "split_keywords": [
        "django"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "45e41066e09a562333f587598ae20e5991beb07751ed94f350c8aae21a476b67",
                "md5": "750d12c9b32809eb3c3c7e925ff86663",
                "sha256": "3016daddfa68428e633b9fd0f26245c7ab88a301bb6373d0423f5c328ec36673"
            },
            "downloads": -1,
            "filename": "django_rich-1.8.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "750d12c9b32809eb3c3c7e925ff86663",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7082,
            "upload_time": "2023-10-11T09:36:19",
            "upload_time_iso_8601": "2023-10-11T09:36:19.073517Z",
            "url": "https://files.pythonhosted.org/packages/45/e4/1066e09a562333f587598ae20e5991beb07751ed94f350c8aae21a476b67/django_rich-1.8.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5b4239c6525d465dbb266547e788622ca35b370c16756e7db9b1cefae7665d11",
                "md5": "7f9585d841ba8f24f4d65377b316758c",
                "sha256": "3121ff32c7c2098ada9dceacfb0fb942c88e7f3e846954cc23008f65af937e9f"
            },
            "downloads": -1,
            "filename": "django_rich-1.8.0.tar.gz",
            "has_sig": false,
            "md5_digest": "7f9585d841ba8f24f4d65377b316758c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 59039,
            "upload_time": "2023-10-11T09:36:20",
            "upload_time_iso_8601": "2023-10-11T09:36:20.641709Z",
            "url": "https://files.pythonhosted.org/packages/5b/42/39c6525d465dbb266547e788622ca35b370c16756e7db9b1cefae7665d11/django_rich-1.8.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-10-11 09:36:20",
    "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.15024s