Skip to content

Commit c13ca10

Browse files
author
Frank Zickert
authored
Create nbimport.py
1 parent 045892f commit c13ca10

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

libs/nbimport.py

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

0 commit comments

Comments
 (0)