Skip to main content

Metakernel magics with TAB completion

Project description

Boosted Magics

Python package that extends the magics of metakernel to provide TAB completion (complete_request messages) on arguments of the magic.

Supports :

  • argument name completion
  • argument value completion, with custom completer function
  • parsing of magic command line with argparse
pip install boosted-magics

Creating a Boosted Magic

Boosted Magic subclasses Metakernel Magic, so the magic declaration is the same. To have argument completion, you have to replace @option decorator of Magic with a @boosted_option, as below :

from boosted_magics import boosted_option, BoostedMagic


class ExampleMagic(BoostedMagic):
    @boosted_option(
        "--very_long_option",
        "-o",
        default=None,
        help="This is a very long option name",
    )
    @boosted_option(
        "--an_other_option_but_which_is_a_flag",
        default=None,
        action="store_true",
        help="This is an other option",
    )
    @boosted_option(
        "-a",
        "--an_option_with_custom_completion_method",
        help="An option with custom completion method",
        completer=lambda w, r: ["customa1", "customa2"],
    )
    def line_example(
        self,
        very_long_option,
        an_other_option_but_which_is_a_flag,
        an_option_with_custom_completion_method,
    ):
        """
        %example : try the boosted magic completion
        """
        self.kernel: TestKernel
        self.kernel.Print(
            f"Example magic successfully called, with args :\n• very_long_option : {very_long_option}\n• an_other_option : {an_other_option_but_which_is_a_flag}\n• an_option_with_custom_completion_method : {an_option_with_custom_completion_method}"
        )


def register_magics(kernel) -> None:
    kernel.register_magics(ExampleMagic)

The boosted_option uses argparse under the hood for parsing (replacing optparse of metakernel), at command execution time. Hence, any parameter that you would give to an argparse argument will work for a boosted_option.

This allows flags (action='store_true'), ...

For argument completion, boosted_magics uses statikomand, a custom package that mimics argparse mechanism for uncomplete commands. Completion is then proposed only for arguments that are not present on the command line.

You can create a custom completer function, that will be called just after you added the argument name (see example above). However, you will have to type at least the first letter of the argument value to get completion proposals.

Troubleshooting

TAB completion for magic can be broken (some completion erase all line) with older metakernel versions (before 1.0.4). To fix it, install the 1.0.4 (or above) manually within the project that uses boosted-magics :

uv add "metakernel @ git+https://github.com/Calysto/metakernel.git@afd3ddd"

or without uv:

git clone https://github.com/Calysto/metakernel
cd metakernel
git checkout afd3ddd
pip install -e .
cd ..

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

boosted_magics-0.0.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boosted_magics-0.0.2-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file boosted_magics-0.0.2.tar.gz.

File metadata

  • Download URL: boosted_magics-0.0.2.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for boosted_magics-0.0.2.tar.gz
Algorithm Hash digest
SHA256 334fd7f6a8d36febbcdc6178083b752127958c97e48ac19d0104717e78668c54
MD5 7592caa8f262aad9851ded43f99ff485
BLAKE2b-256 14043d1df5ce3c405cc4d8caf703d41dd919ab4d47b68603331b16245afed0f8

See more details on using hashes here.

File details

Details for the file boosted_magics-0.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for boosted_magics-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d3c3520cc7a42a191845dfe25eb46eeaba0efcd2454d01e7de93e59c61f7b7f0
MD5 7f8deb85a51c7301f4270958e751c324
BLAKE2b-256 4b94410d1729d3b8b770e46bc692bcd04e100edd6bd960cf26a8b067607ab768

See more details on using hashes here.

Supported by

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