Package: margo-loader
margo-loaderImport Jupyter Notebooks notebooks as Python modulesVideo tutorialLive Coding Demo: Using Jupyter Notebooks as Python Modules with Margo Loader.Demo NotebooksOfficial tutorial (GitHub)on using Margo loader to write Modular Jupyter notebooks.BinderQuick demo notebook (Google Colab)in Google Colaboratory (no install needed).A more realistic suite of notebooks (GitHub)for background deletion and color extraction on William Blake prints, written for the Yale Digital Humanities Lab.BinderInstallationTo install margo-loader, run:pipinstallgit+https://github.com/margo-notebooks/margo-loader-pyImporting a notebookAssuming you have a file called "notebook.ipynb":importmargo_loaderimportnotebookignore-cellNot 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 syntaxhere.An alias forignore-cellisskip. So this does the same thing:# :: skip ::print("This code will not be executed when imported with margo-loader")Creating virtual submodulesYou 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 filetest_notebooks/greetings.ipynbin this repo.# greetings.ipynb# :: submodule: "grumpy" ::defsay_hello(to="world"):returnf"Oh, uhh, hi{to}..."# greetings.ipynb# :: submodule: "nice" ::defsay_hello(to="world"):returnf"Hello,{to}! Nice to see you."Notice we define the samesay_hellofunction twice. If the entire notebook
were imported, the secondsay_hellowould overwrite the first. However, we can
import either of these submodules or both using Python's standard import syntax once we
importmargo_loader.>>>importmargo_loader>>>fromtest_notebooks.greetingsimportnice,grumpy>>>nice.say_hello()'Hello, world! Nice to see you.'>>>grumpy.say_hello()'Oh, uhh, hi world...'>>>Prevent a notebook from being importedTo 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 ado-not-import/not-a-moduledeclaration, it will raise an exception.Skipping multiple cellsIf 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 containsmodule-stopwill be excluded during import until amodule-startcell is encountered.Conversely, any cell including and after a cell that containsmodule-startwill be excluded during import until amodule-stopis encountered.Note that you can also usestartandstopinstead ofmodule-startandmodule-stop. These are aliases.You can usemodule-stopwith no subsequentmodule-start. This will have the effect of ignoring all subsequent cells.Working with percent-formatted notebooksThis 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 attest_notebooks/hello_notebook_pynb.pynbin 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 versionPrior artThis project borrows its implementation approach froma Jupyter Notebook
documentation
examplethat imports notebooks in their entirety as if they were.pyfiles. The key difference Margo Loader adds is use of Margo notes to create preoprocessor directivesignore-cellandsubmodule.