Skip to content Skip to sidebar Skip to footer

How Do I Define Custom Magics In Jupyter?

I'm using Ubuntu 14.04 LTS with an Anaconda python installation: Python 3.5.1 :: Anaconda 2.4.1 (64-bit) I'm trying to use this recipe to enable C++ interactive compiling in my i

Solution 1:

There are two separate things here:

  1. startup files are scripts in ~/.ipython/profile_[name]/startup that are executed as part of starting IPython. They are treated as if you %run each of them prior to the first In[1] prompt. Startup files cannot be imported, because they are not on sys.path.
  2. extensions are Python modules that can be imported and define a load_ipython_extension function. You can put extensions in ~/.ipython/extensions and they will be importable, or you can install them as regular packages with pip.

The first fix is to move your cppmagics to ~/.ipython/extensions or to some site-packages directory, so that it is importable.

If you really want the magics always registered (rather than calling %load_ext cppmagic), you can leave it as a startup file and register the magic at the end of the script, instead of def load_ipython_extension:

if__name__== '__main__':
    from IPython import get_ipython
    get_ipython().register_magics(CppMagics)

Post a Comment for "How Do I Define Custom Magics In Jupyter?"