Skip to main content

Import Jupyter notebooks using Margo notebook margin notebook syntax

Project description

margo-loader

Import Jupyter Notebooks notebooks as Python modules

Video tutorial

Live Coding Demo: Using Jupyter Notebooks as Python Modules with Margo Loader.

Demo Notebooks

Installation

To install margo-loader, run:

pip install git+https://github.com/margo-notebooks/margo-loader-py

Importing a notebook

Assuming you have a file called "notebook.ipynb":

import margo_loader
import notebook

ignore-cell

Not every cell in a Notebook makes sense to include in its module representation.

If you want to prevent a cell from being exported, start the cell with the specially-formatted comment line # :: ignore-cell ::, like this:

# :: ignore-cell ::
print("This code will not be executed when imported with margo-loader")

This special code comment is called a Margo note. Margo notes in Python cells begin with # :: to differentiate them from regular comments, and end with ::.

Learn more about the underlying Margo syntax here.

An alias for ignore-cell is skip. So this does the same thing:

# :: skip ::
print("This code will not be executed when imported with margo-loader")

Creating virtual submodules

You can organize code cells into virtual submodules within a notebook. This in effect allows you to group cells from the same notebook. Here's an example of a few cells from the file test_notebooks/greetings.ipynb in this repo.

# greetings.ipynb
# :: submodule: "grumpy" ::
def say_hello(to="world"):
    return f"Oh, uhh, hi {to}..."
# greetings.ipynb
# :: submodule: "nice" ::
def say_hello(to="world"):
  return f"Hello, {to}! Nice to see you."

Notice we define the same say_hello function twice. If the entire notebook were imported, the second say_hello would overwrite the first. However, we can import either of these submodules or both using Python's standard import syntax once we import margo_loader.

>>> import margo_loader
>>> from test_notebooks.greetings import nice, grumpy
>>> nice.say_hello()
'Hello, world! Nice to see you.'
>>> grumpy.say_hello()
'Oh, uhh, hi world...'
>>>

Prevent a notebook from being imported

To prevent a notebook from being imported, use:

# :: not-a-module ::

or

# :: do-not-import ::

These are currently aliases with the same behavior. If you try to import a notebook that contains a do-not-import/not-a-module declaration, it will raise an exception.

Skipping multiple cells

If you want to ignore a lot of cells during import, you can use

# :: module-stop ::

and

# :: module-start :: 

to exclude blocks of cells.

Any cell including and after a cell that contains module-stop will be excluded during import until a module-start cell is encountered.

Conversely, any cell including and after a cell that contains module-start will be excluded during import until a module-stop is encountered.

Note that you can also use start and stop instead of module-start and module-stop. These are aliases.

You can use module-stop with no subsequent module-start. This will have the effect of ignoring all subsequent cells.

Working with percent-formatted notebooks

This library works with Jupyter Notebooks (.ipynb files) as well as python files with percent cell formatting using the file extension .pynb. These are plain source Python files that use # %% to split the document into cells. Read more here.

Look at test_notebooks/hello_notebook_pynb.pynb in this repo for an example of a code-cell notebook.

STABILITY NOTE: This is an alpha feature. The .pynb extension may be changed in a future version

Prior art

This project borrows its implementation approach from a Jupyter Notebook documentation example that imports notebooks in their entirety as if they were .py files. The key difference Margo Loader adds is use of Margo notes to create preoprocessor directives ignore-cell and submodule.

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

margo_loader-1.0.4.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

margo_loader-1.0.4-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file margo_loader-1.0.4.tar.gz.

File metadata

  • Download URL: margo_loader-1.0.4.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for margo_loader-1.0.4.tar.gz
Algorithm Hash digest
SHA256 54b44d46dd04da1cf38599655989edbc39352764758b4ae24892a052f6688167
MD5 e59a05bc7bf1c375dc2ca4142f32d3b6
BLAKE2b-256 753b9e310e38de36dfdeadf81cd70fa309197debaa79185de4a2a16f4bd0f6bc

See more details on using hashes here.

File details

Details for the file margo_loader-1.0.4-py3-none-any.whl.

File metadata

  • Download URL: margo_loader-1.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for margo_loader-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 280902aa10a04937d5f9af7ac1dcd7adf04c1be65d06ffc53ec3e8ecdb72c870
MD5 9134a5106515f51b3a17e5d788898b3e
BLAKE2b-256 c1318d49bb1313762933ec23b04aba5030d94edec2e8bcfe28d46642d65ddbca

See more details on using hashes here.

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