Skip to main content

Use Typer to define the CLI for your Django management commands.

Project description

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 as clear, DRY, and safely as possible 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.

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',
    ]

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:

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"))
      ],
   ):
      if numbers:
         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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_typer-0.6.0b0.tar.gz (37.7 kB view details)

Uploaded Source

Built Distribution

django_typer-0.6.0b0-py3-none-any.whl (38.2 kB view details)

Uploaded Python 3

File details

Details for the file django_typer-0.6.0b0.tar.gz.

File metadata

  • Download URL: django_typer-0.6.0b0.tar.gz
  • Upload date:
  • Size: 37.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.4 Darwin/23.2.0

File hashes

Hashes for django_typer-0.6.0b0.tar.gz
Algorithm Hash digest
SHA256 9f489ffd77a65d1c47ab0500789ce79387dc162f047b533bde1ce731537a14fd
MD5 85791154634b81a22ca24998a755c814
BLAKE2b-256 6be1ca5a620f2546b111203512bf9893585b757e033b3cf6fcf0aa66c6a87b6c

See more details on using hashes here.

File details

Details for the file django_typer-0.6.0b0-py3-none-any.whl.

File metadata

  • Download URL: django_typer-0.6.0b0-py3-none-any.whl
  • Upload date:
  • Size: 38.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.4 Darwin/23.2.0

File hashes

Hashes for django_typer-0.6.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 8097540a1ba1c6bcf2336a273e508defc23e15ab53976fed2696be64e47c4d32
MD5 cda01e2ef6955d2c3f28bd2ec5814e96
BLAKE2b-256 a33bfde4aca1830ca352b10b8935e2d76437e65b471aa063f79b5ccc7c640b2f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page