Jupyter Notebooks are great for data exploration, visualizing, documenting, prototyping and iteracting with the code, but when it comes to creating an actual program out of a notebook they fall short. I often get myself copying cells from a notebook into a script so that I can run the code with command line arguments. There is no easy way to run a notebook and return a result from its execution, passing arguments to a notebook and running individual code cells programatically. Have you ever wrapped a code cell to a function just so you want to call it in a loop with different paramethers?
I wrote a small utility tool nbloader
that enables code reusing from jupyter notebooks. With it you can import a notebook as an object, pass variables to it's name space, run code cells and pull out variables from its name space.
This tutorial will show you how to make your notebooks resusable with nbloader
.
In [1]:
from nbloader import Notebook
loaded_notebook = Notebook('test.ipynb')
The above commad loades a notebook as an object. This can be done inside a jupyter notebook or a regular python script.
In [2]:
loaded_notebook.run_all()
Out[2]:
After loaded_notebook.run_all()
is called:
In [3]:
loaded_notebook.ns['a']
Out[3]:
In [4]:
loaded_notebook.ns['b']
In [5]:
loaded_notebook.run_tag('add_one')
print(loaded_notebook.ns['a'])
loaded_notebook.run_tag('add_one')
print(loaded_notebook.ns['a'])
If a cell have a comment on its first line it will become a tag.
In [6]:
loaded_notebook.ns['a'] = 0
loaded_notebook.run_tag('add_one')
print(loaded_notebook.ns['a'])
Since It's namespace is just a dic, there is no performance penalty when passing large objects the notebook. All the code from it's cells is compiled and can be called in a loop with the spead of a regular function.