Skip to main content

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

Project description

django-typer

License: MIT PyPI version PyPI pyversions PyPI djversions PyPI status Documentation Status Code Cov Test Status Lint Status Code Style

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:

Please refer to the full documentation for more information.

django-typer example

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 shell completion command to enable tab-completion or if you would like django-typer to install rich traceback rendering 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
        """

Basic Example

Multiple Subcommands Example

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

Multiple Subcommands Example Multiple Subcommands Example - create Multiple Subcommands Example - delete

Grouping and Hierarchies Example

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
./manage.py hierarchy math multiply 10 2
   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}"

Grouping and Hierarchies Example Grouping and Hierarchies Example - math Grouping and Hierarchies Example - math multiply Grouping and Hierarchies Example - math divide

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-1.1.0.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

django_typer-1.1.0-py3-none-any.whl (44.6 kB view details)

Uploaded Python 3

File details

Details for the file django_typer-1.1.0.tar.gz.

File metadata

  • Download URL: django_typer-1.1.0.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.4.0

File hashes

Hashes for django_typer-1.1.0.tar.gz
Algorithm Hash digest
SHA256 8db2bdb751ec64d3a341706f78d1794a8911f4888c43eb498de97e9cf632bdc3
MD5 bab9119491400b407adf00eb043c6258
BLAKE2b-256 3325a8e37d0e2b51dcd1a5b14cbcf5b764392e93723f6453fb4c05139495645b

See more details on using hashes here.

File details

Details for the file django_typer-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: django_typer-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 44.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.12.2 Darwin/23.4.0

File hashes

Hashes for django_typer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f17974d4f6aa6e586970949523e477345d9b64d7b3c4e78c6a7f9a2d564614d
MD5 21053f0ecaccfce8f21185070189c439
BLAKE2b-256 3f144e2d90e65441e25f3f354532496a978a138823fb0388e5dde833ba06212a

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