Skip to main content

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 #!ignore at 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_NAME environment 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

impynb-0.1.5.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

impynb-0.1.5-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

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

Hashes for impynb-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f5888625d37d5dfedbeb26e82991596e0df1335b72c406f4646b0a8cc9a0c195
MD5 fc16ed6f205b03dc6d25eefd9aab6cd6
BLAKE2b-256 3a4eb271b8c0daf12396bac6a2ecbf3afc090f48b6a18128ab4ffaecbd31ced6

See more details on using hashes here.

Provenance

The following attestation bundles were made for impynb-0.1.5.tar.gz:

Publisher: publish-pypi.yml on shirte/impynb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for impynb-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0615f6c99e8ed96eaf916809801693103a95cf0c89ad74455547c20f0b28faa0
MD5 dcb9de4049c37427dd13e70b8fe46f64
BLAKE2b-256 784ede26104960d81a1d90f3504a8ffb3a4a7dec434b6445e61a9c247f701db9

See more details on using hashes here.

Provenance

The following attestation bundles were made for impynb-0.1.5-py3-none-any.whl:

Publisher: publish-pypi.yml on shirte/impynb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page