If you are using Jupyter, you will want to reuse the functions and results of another notebook (ipynb file). There was an official way to import it as a python module, so I will introduce it. (I think I'll use it many times in the future, so I'll include the meaning of my memorandum)
Official site example: http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html
The way to do this is to run notebook, register the result as a module, and use sys.meta_path to hook and call import. The notebook will run when the import is called, but rest assured that it will not change its original state.
Below is the code.
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):
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
nb_path = nb_path.replace("_", " ")
if os.path.isfile(nb_path):
return nb_path
class NotebookLoader(object):
def __init__(self, path=None):
self.shell = InteractiveShell.instance()
self.path = path
def load_module(self, fullname):
path = find_notebook(fullname, self.path)
with io.open(path, 'r', encoding='utf-8') as f:
nb = read(f, 4)
mod = types.ModuleType(fullname)
mod.__file__ = path
mod.__loader__ = self
mod.__dict__['get_ipython'] = get_ipython
sys.modules[fullname] = mod
save_user_ns = self.shell.user_ns
self.shell.user_ns = mod.__dict__
try:
for cell in nb.cells:
if cell.cell_type == 'code':
code = self.shell.input_transformer_manager.transform_cell(cell.source)
exec(code, mod.__dict__)
finally:
self.shell.user_ns = save_user_ns
return mod
class NotebookFinder(object):
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:
key = os.path.sep.join(path)
if key not in self.loaders:
self.loaders[key] = NotebookLoader(path)
return self.loaders[key]
Save this as notebookutil.py etc. When using
import sys
import notebookutil as nbu
sys.meta_path.append(nbu.NotebookFinder())
Then you can import the notebook.
import mynotebook
Of course, you can use functions and classes, but since you are actually executing notebook, you can import variables etc. with the values at the time of execution. Super convenient! !!
Also, since it is running, print, plt.show, etc. are displayed, but if you run the cell that imports the notebook again, it will disappear.
This will give you a more comfortable Jupyter life.
Recommended Posts