No project description provided
Project description
impynb
Import Jupyter notebooks as Python modules!
Installation
pip install impynb
Usage
Assume your project looks like this:
my-project/
my_project/
__init__.py
my_notebook.ipynb
my_module.py
util.py
pyproject.toml
You can import notebooks from Python modules:
# my_module.py
import impynb
from .my_notebook import some_function # <-- import notebook
some_function(3, 4)
Importing local Python modules from notebooks also works:
# cell in my_notebook.ipynb
import impynb
from .util import add_two_numbers # <-- import local Python module
def some_function(a, b):
return add_two_numbers(a, b)
Features
- Async code: Notebook cells containing async code will be executed in the default event loop:
# my_notebook.ipynb from asyncio import sleep await sleep(0.1) variable = 42 # my_module.py import impynb from . import my_notebook my_notebook.variable # 42
The event loop can be configured (see below). - Exception handling: Exceptions thrown in notebooks are displayed correctly in the stack trace
# my_notebook.ipynb (first cell) raise NotImplementedError() # my_module.py import impynb from .my_notebook import * # raises an exception containing the problematic cell: # # File ".../my_module.py", line 2 # from .my_notebook import * # ^^^^^^^^^^^^^^^^^^^^^^^^^^ # ... # File ".../my_notebook.ipynb[cell-0]", line 1, in <module> # raise NotImplementedError()
- Code Cell Filtering: Notebook cells marked with a comment
#!ignoreat the top will be skipped when importing the notebook.#!ignore # this code will not run when importing the notebook some_heavy_computation()
- Advanced configuration: The module can be configured to use different markers for ignorable cells or a custom event loop (see below).
Advanced configuration
The package can be configured
import impynb
impynb.init()
# configuration only applies for imports *after* init() call
from .my_notebook import nice_function
Skip tagged cells
Cells in Jupyter notebooks can be tagged. You can configure impynb to skip executing cells having
a specific tag on them:
import impynb
impynb.init(skip_cell_tags=['test'])
Custom event loop
Executing async code will run in the default event loop if not specified otherwise. However,
impynb can be configured to use a different event loop:
import asyncio
import impynb
my_event_loop = asyncio.new_event_loop()
impynb.init(event_loop=my_event_loop)
# async cells will be run in the custom event loop
from . import my_notebook
Context manager
The package provides a context manager for a temporary reconfiguration of import settings:
import impynb
with impynb.configure(skip_cell_tags=['test']):
# will ignore cells tagged as "test"
from . import my_notebook
# will not ignore cells anymore
import . from my_other_notebook
All arguments of the impynb.init method are also available in impynb.configure.
Compatibility
impynb was tested on the following Jupyter platforms:
- ✅ VSCode: via
__vsc_ipynb_file__variable (source) - ✅ Jupyter Notebooks: via
JPY_SESSION_NAMEenvironment variable or__session__as fallback (source) - ✅ Jupyter Lab: same as Jupyter Notebooks
- ⚪ Papermill: provides no indication about the current notebook name or path (as far as I know) and the user needs to provide this manually (see above)
License
This project is licensed under the BSD 3-Clause "New" or "Revised" License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file impynb-0.1.5.tar.gz.
File metadata
- Download URL: impynb-0.1.5.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5888625d37d5dfedbeb26e82991596e0df1335b72c406f4646b0a8cc9a0c195
|
|
| MD5 |
fc16ed6f205b03dc6d25eefd9aab6cd6
|
|
| BLAKE2b-256 |
3a4eb271b8c0daf12396bac6a2ecbf3afc090f48b6a18128ab4ffaecbd31ced6
|
Provenance
The following attestation bundles were made for impynb-0.1.5.tar.gz:
Publisher:
publish-pypi.yml on shirte/impynb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
impynb-0.1.5.tar.gz -
Subject digest:
f5888625d37d5dfedbeb26e82991596e0df1335b72c406f4646b0a8cc9a0c195 - Sigstore transparency entry: 409094981
- Sigstore integration time:
-
Permalink:
shirte/impynb@0a0136e8d75ba74dd1c263711f594be85920350c -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/shirte
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0a0136e8d75ba74dd1c263711f594be85920350c -
Trigger Event:
release
-
Statement type:
File details
Details for the file impynb-0.1.5-py3-none-any.whl.
File metadata
- Download URL: impynb-0.1.5-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0615f6c99e8ed96eaf916809801693103a95cf0c89ad74455547c20f0b28faa0
|
|
| MD5 |
dcb9de4049c37427dd13e70b8fe46f64
|
|
| BLAKE2b-256 |
784ede26104960d81a1d90f3504a8ffb3a4a7dec434b6445e61a9c247f701db9
|
Provenance
The following attestation bundles were made for impynb-0.1.5-py3-none-any.whl:
Publisher:
publish-pypi.yml on shirte/impynb
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
impynb-0.1.5-py3-none-any.whl -
Subject digest:
0615f6c99e8ed96eaf916809801693103a95cf0c89ad74455547c20f0b28faa0 - Sigstore transparency entry: 409094987
- Sigstore integration time:
-
Permalink:
shirte/impynb@0a0136e8d75ba74dd1c263711f594be85920350c -
Branch / Tag:
refs/tags/v0.1.5 - Owner: https://github.com/shirte
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@0a0136e8d75ba74dd1c263711f594be85920350c -
Trigger Event:
release
-
Statement type: