ipython: New custom magic commands do not work

Hi all,

I was starting to create some magic commands for jupyterlab-requirements extenstion for dependency management following: https://ipython.readthedocs.io/en/stable/config/custommagics.html

I added the new class in https://github.com/thoth-station/jupyterlab-requirements/blob/master/jupyterlab_requirements/__init__.py in local environment for testing:

from IPython.core.magic import magics_class, Magics
from IPython.core.magic import line_magic  # Called with %
from IPython.core.magic import cell_magic  # Called with %%
from IPython.core.magic import line_cell_magic  # Called with % or %%

@magics_class
class HorusMagics(Magics):
    """Horus jupyterlab-requirements CLI as magic commands."""

    @line_magic
    def abra(self, line):
        return line

    @cell_magic
    def show(self, line, cell):
        return line, cell

After starting jupyter lab and testing the command in a notebook, I get: Screenshot from 2021-07-15 13-26-04

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

@pacospace Cool!

If you don’t mind, I’m going to close this issue then.

Thank you a lot @MrMino for all help and support!! 🚀 🥇

@pacospace Cool!

If you don’t mind, I’m going to close this issue then.

@pacospace I’m afraid it might be unsupported, at least not in IPython itself. AFAIK extensions are either bundled with IPython, autoloaded via user’s configuration, or loaded with %load_ext.

If you look at the source, the code responsible for loading extensions is IPython.core.extensions.ExtensionManager.load_extension. There’s no usage of this method outside of %load_ext and unrelated reload methods.

Thanks @MrMino for looking into this!! I think I found a solution, adding logic to the extension for that. Basically when opening any notebook, the command %load_ext jupyterlab_requirements is run automatically: https://github.com/thoth-station/jupyterlab-requirements/blob/01af0c92af31b887f9fd9d22a2151ec4f7a7c14b/src/dependencyManagementButton.ts#L84

No worries, I still have this in mind 😃. I’ll look at it as soon as I have the time to.