|
| 1 | +""" |
| 2 | +from http://stackoverflow.com/questions/20186344/ipynb-import-another-ipynb-file |
| 3 | +""" |
| 4 | +import io, os, sys, types |
| 5 | +from IPython import get_ipython |
| 6 | +from nbformat import current |
| 7 | +from IPython.core.interactiveshell import InteractiveShell |
| 8 | + |
| 9 | + |
| 10 | +def find_notebook(fullname, path=None): |
| 11 | + """find a notebook, given its fully qualified name and an optional path |
| 12 | +
|
| 13 | + This turns "foo.bar" into "foo/bar.ipynb" |
| 14 | + and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar |
| 15 | + does not exist. |
| 16 | + """ |
| 17 | + name = fullname.rsplit('.', 1)[-1] |
| 18 | + if not path: |
| 19 | + path = [''] |
| 20 | + for d in path: |
| 21 | + nb_path = os.path.join(d, name + ".ipynb") |
| 22 | + if os.path.isfile(nb_path): |
| 23 | + return nb_path |
| 24 | + # let import Notebook_Name find "Notebook Name.ipynb" |
| 25 | + nb_path = nb_path.replace("_", " ") |
| 26 | + if os.path.isfile(nb_path): |
| 27 | + return nb_path |
| 28 | + |
| 29 | + |
| 30 | +class NotebookLoader(object): |
| 31 | + """Module Loader for Jupyter Notebooks""" |
| 32 | + def __init__(self, path=None): |
| 33 | + self.shell = InteractiveShell.instance() |
| 34 | + self.path = path |
| 35 | + |
| 36 | + def load_module(self, fullname): |
| 37 | + """import a notebook as a module""" |
| 38 | + path = find_notebook(fullname, self.path) |
| 39 | + |
| 40 | + print ("importing Jupyter notebook from %s" % path) |
| 41 | + |
| 42 | + # load the notebook object |
| 43 | + with io.open(path, 'r', encoding='utf-8') as f: |
| 44 | + nb = current.read(f, 'json') |
| 45 | + |
| 46 | + |
| 47 | + # create the module and add it to sys.modules |
| 48 | + # if name in sys.modules: |
| 49 | + # return sys.modules[name] |
| 50 | + mod = types.ModuleType(fullname) |
| 51 | + mod.__file__ = path |
| 52 | + mod.__loader__ = self |
| 53 | + mod.__dict__['get_ipython'] = get_ipython |
| 54 | + sys.modules[fullname] = mod |
| 55 | + |
| 56 | + # extra work to ensure that magics that would affect the user_ns |
| 57 | + # actually affect the notebook module's ns |
| 58 | + save_user_ns = self.shell.user_ns |
| 59 | + self.shell.user_ns = mod.__dict__ |
| 60 | + |
| 61 | + try: |
| 62 | + for cell in nb.worksheets[0].cells: |
| 63 | + if cell.cell_type == 'code' and cell.language == 'python': |
| 64 | + # transform the input to executable Python |
| 65 | + code = self.shell.input_transformer_manager.transform_cell(cell.input) |
| 66 | + # run the code in themodule |
| 67 | + exec(code, mod.__dict__) |
| 68 | + finally: |
| 69 | + self.shell.user_ns = save_user_ns |
| 70 | + return mod |
| 71 | + |
| 72 | + |
| 73 | +class NotebookFinder(object): |
| 74 | + """Module finder that locates Jupyter Notebooks""" |
| 75 | + def __init__(self): |
| 76 | + self.loaders = {} |
| 77 | + |
| 78 | + def find_module(self, fullname, path=None): |
| 79 | + nb_path = find_notebook(fullname, path) |
| 80 | + if not nb_path: |
| 81 | + return |
| 82 | + |
| 83 | + key = path |
| 84 | + if path: |
| 85 | + # lists aren't hashable |
| 86 | + key = os.path.sep.join(path) |
| 87 | + |
| 88 | + if key not in self.loaders: |
| 89 | + self.loaders[key] = NotebookLoader(path) |
| 90 | + return self.loaders[key] |
| 91 | + |
| 92 | +sys.meta_path.append(NotebookFinder()) |
0 commit comments