首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在pip安装上定义jupyter中的自定义魔法?

如何在pip安装上定义jupyter中的自定义魔法?
EN

Stack Overflow用户
提问于 2016-01-20 16:00:30
回答 1查看 651关注 0票数 0

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

但我希望它是可用的,而不需要进口。我用的是jupyter 4.0.6

下面是代码的一个片段:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-01 10:38:54

因为内核是从MetaKernel派生的,所以可以在Kernel.__init__中注册魔术

代码语言:javascript
复制
from .sasmagic import SASMagic

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

(我假设有一点,因为我看不到您的代码,但是如果您的SASMagic定义在内核类定义旁边的sasmagic.py中,它将是这样的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34904737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档