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.1.tar.gz (2.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.1-py3-none-any.whl (2.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for boosted_magics-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d8c32be4f2286de3074faf0dc0f9441ce85feec90df2ad8191804f73ecb85282
MD5 683b87358efd554bd8eefe7da38b384d
BLAKE2b-256 dc9021bc7cc7e85472b5b6d2892675c0db15087fae3eee263d5c92392a0cc9e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for boosted_magics-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5d8e7efc0a6e7e0ec0080e96177858a3a6c2f627276b8524be5a0368560da6c
MD5 6bb2f4d50cc5f815f53df8b9ae5db623
BLAKE2b-256 5e86c312dd62778bc3e43d75bbd1a5ff125ed50e602e2910a8dd514e28e8ed83

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