0

这是这个问题的扩展, 当我通过 pip 安装我的 SAS 内核时,我想安装我的 SAS Magic。如果我导入包,它将注册from sas_kernel.magics import sas_magic

但我希望它无需导入即可使用。我正在使用 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)
4

1 回答 1

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你的内核类定义旁边。

于 2016-02-01T10:38:54.377 回答