0

これは、SAS カーネルを pip でインストールするときに 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 に答える