Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ipython notebooks:

import IPython.core.magic as ipym

@ipym.magics_class
class CppMagics(ipym.Magics):
    @ipym.cell_magic
    def cpp(self, line, cell=None):
        """Compile, execute C++ code, and return the standard output."""
        # Define the source and executable filenames.
        source_filename = 'temp.cpp'
        program_filename = 'temp.exe'
        # Write the code contained in the cell to the C++ file.
        with open(source_filename, 'w') as f:
            f.write(cell)
        # Compile the C++ code into an executable.
        compile = self.shell.getoutput("g++ {0:s} -o {1:s}".format(
            source_filename, program_filename))
        # Execute the executable and return the output.
        output = self.shell.getoutput(program_filename)
        return output

def load_ipython_extension(ipython):
    ipython.register_magics(CppMagics)

Whether I fire up my notebook with ipython notebok or jupyter notebook (I believe the first aliases to the second anyway), when I execute a cell with :

%load_ext cppmagic

I get the following error :

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-7b90c7a2b808> in <module>()
----> 1 get_ipython().magic('load_ext cppmagic')

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2334         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2335         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2336         return self.run_line_magic(magic_name, magic_arg_s)
   2337 
   2338     #-------------------------------------------------------------------------

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2255                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2256             with self.builtin_trap:
-> 2257                 result = fn(*args,**kwargs)
   2258             return result
   2259 

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)
     64         if not module_str:
     65             raise UsageError('Missing module name.')
---> 66         res = self.shell.extension_manager.load_extension(module_str)
     67 
     68         if res == 'already loaded':

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
     87             if module_str not in sys.modules:
     88                 with prepended_to_syspath(self.ipython_extension_dir):
---> 89                     __import__(module_str)
     90             mod = sys.modules[module_str]
     91             if self._call_load_ipython_extension(mod):

ImportError: No module named 'cppmagic'

The code in the recipe seems to agree with the official docs (both use IPython.core.magic.magics_class) I have placed my cppmagic.py in the following directory

~/.ipython/profile_default/startup

to have it autoload on notebook startup, but I can't get feel the magic. Can anyone help?

like image 943
Lorah Attkins Avatar asked Jan 06 '16 10:01

Lorah Attkins


1 Answers

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)
like image 129
minrk Avatar answered Oct 13 '22 08:10

minrk