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?
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.
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.
You can just enter %run 'NotebookA.ipynb'
in Notebook B and you should be good to go!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With