How to import NoteBook as a module in Jupyter (IPython)

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

How to import NoteBook as a module in Jupyter (IPython)
How to import NoteBook as a module in Jupyter (IPython)
How to execute commands in jupyter notebook
How to revive cells in iPython notebook
How to resolve ModuleNotFoundError: No module named XXX in Jupyter Notebook
How to debug with Jupyter or iPython Notebook
[IPython] How to Share IPython Notebook
How to use IPython Notebook
How to use Jupyter Notebook
How to display DataFrame as a table in Markdown
How to deal with "No module named'〇〇'" error in Jupyter Notebook | Install with! Pip!
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
Post a Jupyter Notebook as a blog post
How to import a file anywhere you like in Python
How to batch start a python program created with Jupyter notebook
How to specify a .py file to load at startup in IPython 0.13
How to get a stacktrace in python
To import your own module with jupyter
How to use jupyter notebook with ABCI
Jupyter Notebook Basics of how to use
How to use Jupyter notebook [Super Basic]
[Small story] How to save matplotlib graphs in a batch with Jupyter
How to fix a bug that jupyter notebook does not start automatically
How to install python package in local environment as a general user
How to set up a jupyter notebook on ssh destination (AWS EC2)
How to hide warnings that do not affect execution in Jupyter Notebook
How to disguise a ZIP file as a PNG file
How to clear tuples in a list (Python)
How to embed a variable in a python string
Summary of how to import files in Python 3
How to create a JSON file in Python
How to implement a gradient picker in Houdini
How to add a Python module search path
To add a module to python put in Julialang
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to output "Ketsumaimo" as standard output in Python
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to uninstall a module installed using setup.py
How to read a file in a different directory
How to Mock a Public function in Pytest
To output a value even in the middle of a cell with Jupyter Notebook
[Linux] [kernel module] How to pass parameters as arguments when loading a loadable kernel module
What to do when the graph does not appear in jupyter (ipython) notebook
What to do if NotADirectoryError: [Errno 20] Not a directory:'xdg-settings' appears in jupyter notebook
Build a PYNQ environment on Ultra96 V2 and log in to Jupyter Notebook
How to make the font width of jupyter notebook put in pyenv equal width
How to use IPython
How to use import
How to specify a schema in Django's database settings
How to convert / restore a string with [] in python
How to run the Ansible module added in Ansible Tower
Try to make a Python module in C language
[Python] How to expand variables in a character string
A memorandum on how to use keras.preprocessing.image in Keras
How to hold a hands-on seminar using Jupyter using docker
Drawing a tree structure with D3.js in Jupyter Notebook
An easy way to create an import module with jupyter
How to instantly launch Jupyter Notebook from the terminal