ipynb文件里面怎样调用另外一个ipynb文件

调用jupyter notebook module
在我们的jupyter notebook文件里调用Ipynb_importer.py,接下来我们就可以像调用普通python文件一样调用其他.ipynb文件里的module了,例如有一个IpynbModule.ipynb文件,里面定义了一个foo函数:
具体参考https://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html

来源于jupyter notebook官方网站:

from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell
def find_notebook(fullname, path=None):
    """find a notebook, given its fully qualified name and an optional path
     This turns "foo.bar" into "foo/bar.ipynb"
    and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar
    does not exist.
    """
    name = fullname.rsplit('.', 1)[-1]
    if not path:
        path = ['']
    for d in path:
        nb_path = os.path.join(d, name + ".ipynb")
        if os.path.isfile(nb_path):
            return nb_path
        # let import Notebook_Name find "Notebook Name.ipynb"
        nb_path = nb_path.replace("_", " ")
        if os.path.isfile(nb_path):
            return nb_path
class NotebookLoader(object):
    """Module Loader for Jupyter Notebooks"""
    def __init__(self, path=None):
        self.shell = InteractiveShell.instance()
        self.path = path

    def load_module(self, fullname):
        """import a notebook as a module"""
        path = find_notebook(fullname, self.path)

        print ("importing Jupyter notebook from %s" % path)

        # load the notebook object
        with io.open(path, 'r', encoding='utf-8') as f:
            nb = read(f, 4)


        # create the module and add it to sys.modules
        # if name in sys.modules:
        #    return sys.modules[name]
        mod = types.ModuleType(fullname)
        mod.__file__ = path
        mod.__loader__ = self
        mod.__dict__['get_ipython'] = get_ipython
        sys.modules[fullname] = mod

        # extra work to ensure that magics that would affect the user_ns
        # actually affect the notebook module's ns
        save_user_ns = self.shell.user_ns
        self.shell.user_ns = mod.__dict__

        try:
          for cell in nb.cells:
            if cell.cell_type == 'code':
                # transform the input to executable Python
                code = self.shell.input_transformer_manager.transform_cell(cell.source)
                # run the code in themodule
                exec(code, mod.__dict__)
        finally:
            self.shell.user_ns = save_user_ns
        return mod
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 提示用户输入一段代码,当用户输入以后执行。这种模式经常被称为REPL(交互式开发环境),或者Read-Eval-P...
    gg5d阅读 5,622评论 0 7
  • 引言 应该使用哪个 IDE/环境/工具?这是人们在做数据科学项目时最常问的问题之一。可以想到,我们不乏可用的选择—...
    liumenghuan阅读 1,941评论 1 7
  • “从来只打天下这等不明道理的我若路见不平,真乃拔刀相助,便死也不怕。”这句话便是无数满腔热血和仗义话承诺侯他...
    星海董廷柁阅读 422评论 0 1
  • 前言 不要嫌前进的慢,只要一直在前进就好。 AlertDialog组件 AlertDialog的功能很强大,它生成...
    olaH阅读 950评论 2 3
  • 1.需求/目的 日志的标准化输出 日志文件的自动创建与拆分 2.使用环境 spring boot 2.0.3 lo...
    东本三月阅读 432评论 0 0