如何将一个Jupyter笔记本导入另一个笔记本 [英] How can I import one Jupyter notebook into another

查看:520
本文介绍了如何将一个Jupyter笔记本导入另一个笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然有可能导入一个Jupyter笔记本到另一个。链接页面有相当多的代码来完成它。我应该将该代码添加到导入笔记本吗?页面不清楚。它应该是一个通用的解决方案,因此将所有代码添加到导入其他笔记本的所有笔记本中没有意义。任何帮助,将不胜感激。谢谢。

Apparently it's possible to import one Jupyter notebook into another. The linked page has quite a bit of code to do it. Am I supposed to add that code to the importing notebook? The page isn't clear about it. It's supposed to be a general solution, so it doesn't make sense to add all that code to all notebooks that import other notebooks. Any help would be appreciated. Thanks.

推荐答案

是的,如果需要,可以将所有代码添加到笔记本中。

Yes, you can add all of that code to a notebook if you want.

是的,你不应该这样做作为一般解决方案。

And yes, you shouldn't do so as a general solution.

笔记本是一个复杂的结构,正如在细节中提到的那样。文字(我认为是JSON)。它可以包含python代码,它可以包含魔法 - cython,bash,latex等等 - Python内核无法理解。基本上你必须复制普通Python导入过程的一部分功能,因为本机Python不会理解Ipython笔记本中有Python代码。

A notebook is a complicated structure, as alluded to in the details of the text (I think it's JSON). It can contain python code, it can contain magics - cython, bash, latex and more - which would not be understood by the Python kernel. Essentially you have to replicate a portion of the functionality of the normal Python import process, as natively Python won't understand there is Python code inside an Ipython notebook.

但是。 ..通常,如果您有大量的Python代码,您可以将其拆分为模块,然后导入模块。这些工作正常,因为它是一个普通的Python导入。

However ... normally if you have a significant amount of Python code you would split it into modules, and then import the modules. These work as normal, because it is a normal Python import.

例如,一旦加载代码告诉它如何理解笔记本是什么,实际导入只是

For example, once the code has been loaded to tell it how to understand what a notebook is, the actual import is only

import nbpackage.mynotebook

我们可以使用与模块导入代码相同的技术 - find_notebook NotebookLoader 可以放入辅助模块(例如 helper.py ),您需要做的就是从笔记本中使用来自帮助程序导入NotebookFinder的

We can use the same technique with the module import code - find_notebook and NotebookLoader can be put into a helper module (e.g. helper.py), and all you would have to do is, from within your notebook, use from helper import NotebookFinder.

我怀疑你还是要从里面调用 sys.meta_path.append(NotebookFinder())您的笔记本以及导入。

I suspect you'd still have to call sys.meta_path.append(NotebookFinder()) from inside your notebook along with the import.

以下是如何使用导入功能创建从笔记本中绘制的API的具体示例:

Here is a specific example of how you can use the import capabilities to create an API drawn from a notebook:

创建一个笔记本。我们称之为 scanner.ipynb

Create a notebook. We'll call it scanner.ipynb:

import os, sys
def scanner(start):
    for root, dirs,files in os.walk(start):
        # remove any already processed file
        if 'done' in dirs:
            dirs.remove('done')
        for names in files:
            name, ext = os.path.splitext(names)
            # only interested in media files
            if ext == '.mp4' or ext == '.mkv':
                print(name)

创建一个名为 reuse.py 的常规python文件。 这是您一般可重复使用的Ipython导入模块

Create a regular python file called reuse.py. This is your general re-usable Ipython import module:

#! /usr/env/bin python
# *-* coding: utf-8 *-*

import io, os, sys, types
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

class NotebookFinder(object):
    """Module finder that locates Jupyter Notebooks"""
    def __init__(self):
        self.loaders = {}

    def find_module(self, fullname, path=None):
        nb_path = find_notebook(fullname, path)
        if not nb_path:
            return

        key = path
        if path:
            # lists aren't hashable
            key = os.path.sep.join(path)

        if key not in self.loaders:
            self.loaders[key] = NotebookLoader(path)
        return self.loaders[key]

创建连接的特定API文件以上笔记本以上的笔记本。称之为 scan_api.py

Create your specific API file that connects the loader above with the notebook above. Call it scan_api.py:

# Note the python import here
import reuse, sys

# This is the Ipython hook
sys.meta_path.append(reuse.NotebookFinder())
import scanner
# And now we can drawn upon the code
dir_to_scan = "/username/location"
scanner.scanner(dir_to_scan)

这篇关于如何将一个Jupyter笔记本导入另一个笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆