Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a notebook in another notebook in IPython?

I have a notebook which I intend to use as a library for other notebooks. It has some functions that can ready certain types of files etc.

How can I include that notebook in a new notebook that should be able to use the functions in the library notebook?

like image 667
kasperhj Avatar asked Sep 29 '13 19:09

kasperhj


People also ask

How do I run a notebook in another notebook?

Running a Jupyter Notebook from Another Jupyter NotebookFrom the left Sidebar, select and right-click on the Jupyter notebook that has to be run from another notebook. From the context menu, select Copy Path. Open the Jupyter notebook from which you want to run another notebook. Click Run.

Can you import one Jupyter Notebook into another?

Fortunately, Python provides some fairly sophisticated hooks into the import machinery, so we can actually make Jupyter notebooks importable without much difficulty, and only using public APIs. Import hooks typically take the form of two objects: a Module Loader, which takes a module name (e.g. 'IPython.


3 Answers

You can just enter %run 'NotebookA.ipynb' in Notebook B and you should be good to go!

like image 136
Mike Chan Avatar answered Oct 17 '22 00:10

Mike Chan


googling for notebook import hook yield some examples. This is still experimental, but you are welcomed to improved it. I would suggest also using the --script flag that save an importable .py file every time you save the notebook.

like image 30
Matt Avatar answered Oct 16 '22 23:10

Matt


With import_ipynb library using A.ipynb from B.ipynb is as simple as writing

import import_ipynb
import A

in B.ipynb.

Actually this library is just one file I've taken from the official jupyter howto and wrapped into a package installable via

pip install import-ipynb

It's also possible to write from A import foo, from A import * and so on.

like image 2
axil Avatar answered Oct 17 '22 00:10

axil