Skip to main content

MIT license PyPI version fury.io PyPI pyversions PyPI djversions PyPI status Documentation Status Code Cov Test Status Lint Status Code Style

django-typer

Use Typer to define the CLI for your Django management commands. Provides a TyperCommand class that inherits from BaseCommand and allows typer-style annotated parameter types. All of the BaseCommand functionality is preserved, so that TyperCommand can be a drop in replacement.

django-typer makes it easy to:

  • Define your command CLI interface in a clear, DRY and safe way using type hints

  • Create subcommand and group command hierarchies.

  • Use the full power of Typer’s parameter types to validate and parse command line inputs.

  • Create beautiful and information dense help outputs.

  • Configure the rendering of exception stack traces using rich.

  • Install shell tab-completion support for TyperCommands and normal Django commands for bash, zsh, fish and powershell.

  • Create custom and portable shell tab-completions for your CLI parameters.

  • Refactor existing management commands into TyperCommands because TyperCommand is interface compatible with BaseCommand.

Please refer to the full documentation for more information.

https://raw.githubusercontent.com/bckohan/django-typer/main/doc/source/_static/img/closepoll_example.gif

Installation

  1. Clone django-typer from GitHub or install a release off PyPI:

    pip install django-typer

    rich is a powerful library for rich text and beautiful formatting in the terminal. It is not required, but highly recommended for the best experience:

    pip install "django-typer[rich]"
  2. Add django_typer to your INSTALLED_APPS setting:

    INSTALLED_APPS = [
        ...
        'django_typer',
    ]

    You only need to install django_typer as an app if you want to use the shellcompletion command to enable tab-completion or if you would like django-typer to install `rich traceback rendering <https://django-typer.readthedocs.io/en/latest/howto.html#configure-rich-stack-traces>`_ for you - which it does by default if rich is also installed.

Basic Example

For example TyperCommands can be a very simple drop in replacement for BaseCommands. All of the documented features of BaseCommand work!

from django_typer import TyperCommand


class Command(TyperCommand):

   def handle(self, arg1: str, arg2: str, arg3: float = 0.5, arg4: int = 1):
      """
      A basic command that uses Typer
      """
https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/basic.svg

Multiple Subcommands Example

Or commands with multiple subcommands can be defined:

import typing as t

from django.utils.translation import gettext_lazy as _
from typer import Argument

from django_typer import TyperCommand, command


class Command(TyperCommand):
   """
   A command that defines subcommands.
   """

   @command()
   def create(
      self,
      name: t.Annotated[str, Argument(help=_("The name of the object to create."))],
   ):
      """
      Create an object.
      """

   @command()
   def delete(
      self, id: t.Annotated[int, Argument(help=_("The id of the object to delete."))]
   ):
      """
      Delete an object.
      """
https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/multi.svg https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/multi_create.svg https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/multi_delete.svg

Grouping and Hierarchies Example

Or more complex groups and subcommand hierarchies can be defined. For example this command defines a group of commands called math, with subcommands divide and multiply. The group has a common initializer that optionally sets a float precision value. We would invoke this command like so:

./manage.py hierarchy math --precision 5 divide 10 2.1
4.76190
./manage.py hierarchy math multiply 10 2
20.00

Any number of groups and subcommands and subgroups of other groups can be defined allowing for arbitrarily complex command hierarchies.

import typing as t
from functools import reduce

from django.utils.translation import gettext_lazy as _
from typer import Argument, Option

from django_typer import TyperCommand, group


class Command(TyperCommand):

   help = _("A more complex command that defines a hierarchy of subcommands.")

   precision = 2

   @group(help=_("Do some math at the given precision."))
   def math(
      self,
      precision: t.Annotated[
         int, Option(help=_("The number of decimal places to output."))
      ] = precision,
   ):
      self.precision = precision

   @math.command(help=_("Multiply the given numbers."))
   def multiply(
      self,
      numbers: t.Annotated[
         t.List[float], Argument(help=_("The numbers to multiply"))
      ],
   ):
      return f"{reduce(lambda x, y: x * y, [1, *numbers]):.{self.precision}f}"

   @math.command()
   def divide(
      self,
      numerator: t.Annotated[float, Argument(help=_("The numerator"))],
      denominator: t.Annotated[float, Argument(help=_("The denominator"))],
      floor: t.Annotated[bool, Option(help=_("Use floor division"))] = False,
   ):
      """
      Divide the given numbers.
      """
      if floor:
            return str(numerator // denominator)
      return f"{numerator / denominator:.{self.precision}f}"
https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/hierarchy.svg https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/hierarchy_math.svg https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/hierarchy_math_multiply.svg https://raw.githubusercontent.com/bckohan/django-typer/main/django_typer/examples/helps/hierarchy_math_divide.svg

Release files for django-typer 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-typer 1.0.3
File Size Uploaded
django_typer-1.0.3.tar.gz 43.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-typer 1.0.3
File Interpreter ABI Platform
django_typer-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size:87.9 kB

Release files / django_typer-1.0.3.tar.gz

Download URL django_typer-1.0.3.tar.gz
Size 43.7 kB
Tags Source
SHA-256 checksum
How to use checksums
365b75f52286f04b588e47131ebbb8d907a5bc0c13527ac5d228bbe40f30f67d
BLAKE2b-256 checksum
How to use checksums
a29ffb66dbc4c534ece3e6d106633cd11f11e1c55159fb75ee1051a9e34f0f99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.0 CPython/3.11.4 Darwin/23.2.0

Release files / django_typer-1.0.3-py3-none-any.whl

Download URL django_typer-1.0.3-py3-none-any.whl
Size 44.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8ca4dd82f1f90ada375b922de5d7691d7645855619a02b39893579544b6d26ef
BLAKE2b-256 checksum
How to use checksums
0fc5b656bbad6ef3a18df9f8f41da467c51929aac20245c089cc11c8315b6044
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.0 CPython/3.11.4 Darwin/23.2.0

Release history Release notifications | RSS feed

4.1.0

2 release files

4.0.0

2 release files

3.9.0

2 release files

3.8.0

2 release files

3.7.4

2 release files

3.7.3

2 release files

3.7.2

2 release files

3.7.1

2 release files

3.7.0

2 release files

3.6.6

2 release files

3.6.5

2 release files

3.6.4

2 release files

3.6.3

2 release files

3.6.2

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.1

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.2

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.2

2 release files

3.2.1

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.1.0

2 release files

3.0.0

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page