2016-01-20 34 views
0

这是此扩展的question 我想通过pip安装我的SAS内核时安装SAS Magic。 它会注册,如果我进口包from sas_kernel.magics import sas_magic如何在pip安装中定义jupyter中的自定义魔术

但我希望它可以不需要导入。 我使用jupyter 4.0.6

下面是代码片段:

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) 
+0

是sas_kernel是IPython内核的衍生物吗?如果是这样,在你的子类中,你可以将它添加到内核的初始化中。 – minrk

+0

@minrk sas_kernel是metakernel的衍生产品,它是内核的衍生产品。所以你说我可以将它添加到sas_kernel的__init__方法中,它会在运行'pip install sas_kernel'之后注册?对于单选问题抱歉,sas_kernel .__ init__中的代码是什么样的? – Jared

回答

0

由于您的内核是从MetaKernel导出,你可以在你Kernel.__init__注册魔法:

from .sasmagic import SASMagic 

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

(我假设了一点点,因为我看不到你的代码,但是它看起来像这样,假设你的SASMagic定义在你的内核类定义旁边的sasmagic.py

+0

非常感谢! – Jared