0

This is an extension of this question I want to install my SAS Magic when I install my SAS Kernel through pip. It will register if I import the package from sas_kernel.magics import sas_magic

but I want it to be available without needing the import. I'm using jupyter 4.0.6

Here is a snippet of the code:

from __future__ import print_function
import IPython.core.magic as ipym
from saspy.SASLogLexer import *
import re
import os

@ipym.magics_class
class SASMagic(ipym.Magics):

    @ipym.cell_magic
    def SAS(self,line,cell):
        '''
        %%SAS - send the code in the cell to a SAS Server
        '''
        executable = os.environ.get('SAS_EXECUTABLE', 'sas')
        if executable=='sas':
            executable='/usr/local/SASHome/SASFoundation/9.4/sas'
        e2=executable.split('/')
        _path='/'.join(e2[0:e2.index('SASHome')+1])
        _version=e2[e2.index('SASFoundation')+1]
        import saspy as saspy
        self.mva=saspy.SAS_session()
        self.mva._startsas(path=_path, version=_version)

        res=self.mva.submit(cell,'html')
        output=self._clean_output(res['LST'])
        log=self._clean_log(res['LOG'])
        dis=self._which_display(log,output)
        return dis

from IPython import get_ipython
get_ipython().register_magics(SASMagic)
Community
  • 1
  • 1
Jared
  • 63
  • 6
  • is sas_kernel a derivative of the IPython kernel? If so, in your subclass you can add this to the initialization of the kernel. – minrk Jan 20 '16 at 16:28
  • @minrk sas_kernel is derivative of metakernel which is a derivative of kernel. So you're saying I can add it in the __init__ method of sas_kernel and it will be registered after running `pip install sas_kernel `? Sorry for the elemenary question, what would the code look like in sas_kernel.__init__? – Jared Jan 28 '16 at 20:33

1 Answers1

0

Since your Kernel is derived from MetaKernel, you can register the magics in your Kernel.__init__:

from .sasmagic import SASMagic

class SASKernel(MetaKernel):
    def __init__(self, **kwargs):
        super(SASKernel, self).__init__(**kwargs)
        self.register_magics(SASMagic)

(I'm assuming a little bit, since I can't see your code, but it would look something like this, assuming your SASMagic definition is in a sasmagic.py next to your kernel class definition.

minrk
  • 37,545
  • 9
  • 92
  • 87