Including a notebook in another notebook in IPytho

2019-02-13 20:15发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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.