[英]How can I import one Jupyter notebook into another
Apparently it's possible to import one Jupyter notebook into another.显然可以将一个 Jupyter 笔记本导入另一个。 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.是的,您不应该将其作为通用解决方案。
A notebook is a complicated structure, as alluded to in the details of the text (I think it's JSON).笔记本是一种复杂的结构,正如文本细节中所暗示的那样(我认为它是 JSON)。 It can contain python code, it can contain magics - cython, bash, latex and more - which would not be understood by the Python kernel.
它可以包含 Python 代码,也可以包含魔法——cython、bash、latex 等等——Python 内核无法理解这些魔法。 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 本身无法理解 Ipython 笔记本中的 Python 代码。
However ... normally if you have a significant amount of Python code you would split it into modules, and then import the modules.但是……通常,如果您有大量 Python 代码,您会将其拆分为模块,然后导入模块。 These work as normal, because it is a normal Python import.
这些工作正常,因为它是一个正常的 Python 导入。
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
We can use the same technique with the module import code - find_notebook
and NotebookLoader
can be put into a helper module (eg helper.py
), and all you would have to do is, from within your notebook, use from helper import NotebookFinder
.我们可以对模块导入代码使用相同的技术 -
find_notebook
和NotebookLoader
可以放入辅助模块(例如helper.py
)中,您所要做的就是在笔记本中使用from helper import NotebookFinder
。
I suspect you'd still have to call sys.meta_path.append(NotebookFinder())
from inside your notebook along with the import.我怀疑您仍然需要从笔记本内部调用
sys.meta_path.append(NotebookFinder())
以及导入。
Here is a specific example of how you can use the import capabilities to create an API drawn from a notebook:下面是一个具体示例,说明如何使用导入功能创建从笔记本中提取的 API:
Create a notebook.创建一个笔记本。 We'll call it
scanner.ipynb
:我们将其称为
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)
Create a regular python file called reuse.py
.创建一个名为
reuse.py
的常规 python 文件。 This is your general re-usable Ipython import module :这是您通用的可重用 Ipython 导入模块:
#! /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]
Create your specific API file that connects the loader above with the notebook above.创建您的特定 API 文件,将上面的加载程序与上面的笔记本连接起来。 Call it
scan_api.py
:称之为
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)
Simple Solution with 2 lines of code 2行代码的简单解决方案
Use ' nbimporter ' package in python for importing another notebook A (or its function) in notebook B. You can install 'nbimporter' by using the command - pip install nbimporter使用python中的' nbimporter '包在笔记本B中导入另一个笔记本A(或其功能)。您可以使用命令-pip install nbimporter安装'nbimporter'
Assume there are two Notebooks A.ipynb and B.ipynb.假设有两个笔记本 A.ipynb 和 B.ipynb。 we are trying to import Notebook A (or its function) inside B Code sample below should solve the issue.
我们正在尝试在 B 中导入 Notebook A(或其功能) 下面的代码示例应该可以解决问题。
Inside Notebook B.ipynb内部笔记本B.ipynb
import nbimporter
import A # or do this --> from A import func1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.