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

How to use another notebook function with Jupyter notebook is also introduced on the official website and Qiita, but I will show you how to make it a little more convenient.

It's basically the same as the official website. https://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.html

Qiita also has a good introductory article. https://qiita.com/tdual/items/32d3918b4c8dd1f703e7

However, the difficulty is that all the contents of the notebook are executed when importing. I want to use only the variables and functions defined in some cells instead of all cells in other notebooks! In fastai's nbdev, in such a case, mark "#export" at the beginning of the cell. But I don't want to exaggerate like nbdev.

So, modify the NotebookLoader class of the program on the official website so that only the cells marked "#export" at the beginning are imported.

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)
                #At the beginning of the cell"#export"Execute only the cell described as
                if code.startswith("#export"):
                    exec(code, mod.__dict__)
        except Exception as ex:
            #In the cell"#export"I often forget to add, so let me know where the error occurred
            print(code)
            raise ex
        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:
            # 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]

Save this with a file name such as nb_finder.py. When using

import sys
import nb_finder as nbf
sys.meta_path.append(nbf.NotebookFinder())

Then you can import other notebooks after that.

from mynotebook import *

In the import source notebook, add "#export" to the beginning of the cell that describes the variables and functions that you want to use in other notebooks. Don't forget to add #export to the cell that has the import statement needed to execute that cell as well. You may also want to specify #hide for an unwanted cell instead of #export to prevent that cell from being imported.

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
How to use IPython Notebook
How to use Jupyter Notebook
How to display DataFrame as a table in Markdown
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 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 clear tuples in a list (Python)
How to embed a variable in a python string
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
[Python] How to draw a histogram in Matplotlib
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
How to resolve SSL module errors in Anaconda environment
How to use jupyter lab in Windows 10 local environment
How to (force) pdf conversion of IPython Notebook slides
How to execute a command using subprocess in Python
How to reference static files in a Django project
How to view progress bar on Jupyter Notebook to see progress
[Linux] How to put your IP in a variable
How to quickly create a machine learning environment using Jupyter Notebook with UbuntuServer 16.04 LTS