https://github.com/ipython/ipynb
Package / Module importer for importing code from Jupyter Notebook files (.ipynb)
https://github.com/ipython/ipynb
Last synced: 5 months ago
JSON representation
Package / Module importer for importing code from Jupyter Notebook files (.ipynb)
- Host: GitHub
- URL: https://github.com/ipython/ipynb
- Owner: ipython
- License: bsd-3-clause
- Created: 2016-11-06T06:23:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-15T14:14:54.000Z (over 1 year ago)
- Last Synced: 2025-03-30T10:07:13.577Z (9 months ago)
- Language: Jupyter Notebook
- Homepage: https://ipynb.readthedocs.io/en/latest/
- Size: 85.9 KB
- Stars: 253
- Watchers: 11
- Forks: 71
- Open Issues: 33
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ipynb
[](https://travis-ci.org/yuvipanda/ipynb)
A python package providing an easy way to explicitly import [Jupyter Notebooks](https://github.com/jupyter/notebook) files (`.ipynb`) the same way you would import regular `.py` files.
## Installation ##
You can install ipynb with:
```bash
pip install ipynb
```
## Importing a notebook ##
### Full import ###
You can do a 'full' import - this has the same semantics of importing a .py file. All the code in the .ipynb file is executed, and classes/functions/variables in the top level are available for use.
If you have a notebook file named `server.ipynb`, you can import it via:
```python
import ipynb.fs.full.server
```
You can use the `from ... import ..` too.
```python
from ipynb.fs.full.server import X, Y, X
```
### Definitions only import ###
Sometimes your notebook has been used as a way to run an analysis or other computation, and you only want to import the functions / classes defined in it - and not the extra statements you have in there. This can be accomplished via `ipynb.fs.defs`.
If you have a notebook file named `server.ipynb`, and do:
```python
import ipynb.fs.defs.server
```
It'll only execute and make available the following parts of the code in `server.ipynb`:
- `class` definitions
- `def` function definitions
- `import` statements
- Assignment statements where the variables being assigned to are ALL_CAPS. These are assumed to be constants.
This skips most computational work and brings in your definitions only, making it easy to reuse functions / classes across similar analyses.
### Relative Imports ###
You can also easily do relative imports, both for full notebooks or for definitions only. This works inside notebooks too.
If you have a notebook called `notebook1.ipynb` in the same dir as your current notebook, you can import it with:
```python
import ipynb.fs # Boilerplate required
# Do a full import
from .full.notebook1 import foo
# Do a definitions-only import
from .defs.notebook1 import bar
```
This works transitively nicely - other code can import your notebook that's using relative imports and it'll all work well.