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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

Supported by

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