Skip to main content

Import .ipynb files as modules in the system path.

Project description

importnb supports the ability to use Jupyter notebooks as python source.

BinderBuild Status

pip install importnb

Jupyter Extension

%load_ext importnb    
    foo = 42
    import readme
    assert readme.foo is 42
    assert readme.__file__.endswith('.ipynb')

Notebooks maybe reloaded with the standard Python Import machinery.

    from importnb import Notebook, reload
    reload(readme);

Unload the extension

%unload_ext importnb

Context Manager

    with Notebook(): 
        import readme
        reload(readme)

Integrations

importnb integrates with IPython, py.test, and setuptools.

IPython

importnb may allow notebooks to import by default with

ipython -c "__import__('importnb').utils.ipython.install()"

This extension will install a script into the default IPython profile startup that is called each time an IPython session is created.

Command

After the importnb extension is created notebooks can be execute from the command line.

ipython -m readme

Unloading the Extension

The default settings may be discarded temporarily with

%unload_ext importnb

py.test

importnb installs a pytest plugin when it is setup. Any notebook obeying the py.test discovery conventions can be used in to pytest. This is great because notebooks are generally your first test.

Setuptools

importnb provides a setuptool command that will place notebooks in a source distribution. In setuptools, update the command classs with

from importnb.utils.setup import build_ipynb
setup(
    ...,
    cmdclass=dict(build_py=build_ipynb)
    ...,)

Developer

    if __name__ == '__main__':
        from pathlib import Path
        import black
        from nbconvert.exporters.markdown import MarkdownExporter
        from importnb.compiler_python import ScriptExporter
        for path in Path('src/notebooks/').rglob("""*.ipynb"""):

            'checkpoint' not in str(path) and (Path('src/importnb') / path.with_suffix('.py').relative_to('src/notebooks')).write_text(
                black.format_str(ScriptExporter().from_filename(path)[0], 100))
        for path in map(Path, ('readme.ipynb', 'changelog.ipynb')):
            path.with_suffix('.md').write_text(MarkdownExporter().from_filename(path)[0])

        __import__('unittest').main(module='tests', argv="discover --verbose".split(), exit=False)
test_import (tests.test_.TestContext) ... ok
test_reload_with_context (tests.test_.TestContext) ... ok
test_reload_without_context (tests.test_.TestContext) ... skipped 'importnb is probably installed'
test_failure (tests.test_.TestExtension) ... expected failure
test_import (tests.test_.TestExtension) ... ok
test_exception (tests.test_.TestPartial) ... ok
test_traceback (tests.test_.TestPartial) ... ok
test_imports (tests.test_.TestRemote) ... skipped 'requires IP'

----------------------------------------------------------------------
Ran 8 tests in 2.021s

OK (skipped=2, expected failures=1)

CHANGELOG

0.2.4

  • Use tox for testing
  • Use a source directory folder structure for pytest and tox testing.
  • Create a pytest plugin that discovers notebooks as tests. With importnb notebooks can be used as fixtures in pytest.
  • Install importnb as an IPython extension.
  • Support running notebooks as modules from the ipython command line
  • Create a setuptools command to allow notebooks as packages.

0.2.1

  • importnb supports notebook inputs from pure python environments. Two compatible compiler were created from IPython and Python

  • importnb.Partial works appropriately by improving exceptions.

  • All of the IPython magic syntaxes were removed to support Pure Python.

  • The generated Python files are formatted with black.

  • Tests were added to:

    • Validate the line number in tracebacks
    • Test someone elses notebooks

0.1.4

  • Pypi supports markdown long_description with the proper mimetype in long_description_content_type.

0.1.3

  • Include the RST files in the MANIFEST.in.

0.1.2 (Unreleased)

  • Use RST files to improve the literacy of the pypi description.

0.1.1

  • Released on PyPi

0.0.2

  • Initial Testing Release

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

importnb-0.1.5.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

importnb-0.1.5-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file importnb-0.1.5.tar.gz.

File metadata

  • Download URL: importnb-0.1.5.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for importnb-0.1.5.tar.gz
Algorithm Hash digest
SHA256 1b419b27f0e5f4e6b51122aed7f1c4f951a3466b14b293d14158e51db2a6cd11
MD5 a65fa5b0e2a531f1b1c522cc70c47d10
BLAKE2b-256 291608be2dc38b2e8c1fa1461bc515f49e660fbf03aacb9c0caa9c32deef5c38

See more details on using hashes here.

File details

Details for the file importnb-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for importnb-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 93e08756fe1ed39fc9789fe521b3d17a0948782f22ae995386708df15a15450c
MD5 6e6fe1a97912a28677a073ef493cf949
BLAKE2b-256 7f38f0f53d5e45693f03b87b3038229a617b9f5ac1832416ee135dda0376ea65

See more details on using hashes here.

Supported by

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