How do I define custom magics in jupyter?

前端 未结 1 370
夕颜
夕颜 2021-01-14 17:24

I\'m using Ubuntu 14.04 LTS with an Anaconda python installation:

Python 3.5.1 :: Anaconda 2.4.1 (64-bit)

I\'m tryi

相关标签:
1条回答
  • 2021-01-14 18:07

    There are two separate things here:

    1. startup files are scripts in ~/.ipython/profile_[name]/startup that are executed as part of starting IPython. They are treated as if you %run each of them prior to the first In[1] prompt. Startup files cannot be imported, because they are not on sys.path.
    2. extensions are Python modules that can be imported and define a load_ipython_extension function. You can put extensions in ~/.ipython/extensions and they will be importable, or you can install them as regular packages with pip.

    The first fix is to move your cppmagics to ~/.ipython/extensions or to some site-packages directory, so that it is importable.

    If you really want the magics always registered (rather than calling %load_ext cppmagic), you can leave it as a startup file and register the magic at the end of the script, instead of def load_ipython_extension:

    if __name__ == '__main__':
        from IPython import get_ipython
        get_ipython().register_magics(CppMagics)
    
    0 讨论(0)
提交回复
热议问题