Skip to main content

Create Jupyter notebooks from Markdown and Python scripts.

Project description

The nbless python package

Using nbless you can create and execute Jupyter notebooks in

  • your terminal or
  • your favorite Python environment (e.g. PyCharm or Visual Studio Code).

The nbless python package consists of 3 functions:

  • nbuild, which creates a notebook from python scripts and plain text files, e.g. markdown (.md) and text (.txt) files.
  • nbexec, which runs a notebook from top to bottom and saves an executed version, leaving the source notebook untouched.
  • nbless, which calls nbuild and nbexec to create and execute a notebook.

These functions rely on the nbconvert and nbformat modules that are included with jupyter.

Basic usage: terminal

Creating and executing a notebook with nbless in the terminal

Run nbless.py script in your terminal, providing all of the names of the source files as arguments, e.g.

python nbless.py README.md plot.py notes.txt

First, the contents of Python code files (.py) are stored as Jupyter notebook code cells, while the contents of all other files are stored in markdown cells.

Then, the newly created notebook is copied, run from top to bottom and saved. The default name of the first notebook is raw.ipynb while the executed notebook is called out.ipynb by default output. The default filepath where the notebooks are saved is the current directory ('./').

You can provide more descriptive names for the notebooks and set a different path:

python nbless.py README.md plot.py notes.txt --raw unexecuted --out executed.ipynb --path notebooks/
# Or
python nbless.py README.md plot.py notes.txt -r not_executed.ipynb -o executed.ipynb -p notebooks/

Creating a notebook with nbuild in the terminal

If you do not want an executed version of the notebook, run nbuild.py instead of nbless.py.

python nbuild.py README.md plot.py notes.txt

The default output filename for nbuild is raw.ipynb. The default output filepath is the current directory ('./').

You can provide a more descriptive filename (-o) and set a different path (-p):

python nbuild.py README.md plot.py -o not_executed.ipynb -p notebooks/
# Or
python nbuild.py README.md plot.py --out not_executed.ipynb --path notebooks/

If you only want to execute a notebook, run nbexec.py.

python nbexec.py raw.ipynb

The default output filename for nbexec.py is out.ipynb. The default output filepath is the current directory ('.').

You can provide more descriptive names for the output name (-o) and path (-p):

python nbexec.py raw.ipynb -o executed.ipynb -p notebooks/
# Or
python nbexec.py raw.ipynb --out executed.ipynb --path notebooks/

Basic usage: python environment

from nbuild import nbuild
from nbexec import nbexec
from nbless import nbless

nbuild(["README.md", "plot.py", "notes.txt"], output_path="notebooks/")
nbexec("notebooks/raw.ipynb", output_path="notebooks/")

# Or to run both nbuild and nbexec at once, use nbless
nbless(["README.md", "plot.py", "notes.txt"], nbexec_path="notebooks/")

Missing a dependency?

If you installed Anaconda, you should already have all of the dependencies (python, nbformat, and nbconvert).

If not, or if you have Miniconda installed, run

conda install -yc conda-forge jupyter

If you have any other Python installation, run

pip install jupyter

Too many file names to type out?

You can use the ls command to assign all of the relevant names in the current directory to a variable and pass this variable as an argument to nbconvert.py.

To preserve the order and differentiate files that should be incorporated into the notebook, I recommend left padding your file names with zeros (e.g. 01_intro.md, 02_figure1.py).

Consider the example below:

touch {01..09}.py
name_list=`ls 0*.py`
python nbuild.py `echo $name_list`

In a python environment, I recommend os.listdir to obtain a list of all files:

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

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

nbless-0.0.2.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

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

nbless-0.0.2-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file nbless-0.0.2.tar.gz.

File metadata

  • Download URL: nbless-0.0.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for nbless-0.0.2.tar.gz
Algorithm Hash digest
SHA256 4084623456a592bdadd2dda420eedf666dcef8ae266aed9e3a91a1e4a13ea373
MD5 c363967d4ca08634e66d25a197f90720
BLAKE2b-256 75c6ac4e3fd13f9c6bc4cbb40da4808db6be0cd1d41e5a9a9662e2407d529c01

See more details on using hashes here.

File details

Details for the file nbless-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: nbless-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 2.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

File hashes

Hashes for nbless-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 67bab66b64d4e72bd815008d5942f31101a50ae183bd42c53440801c09b5eb5f
MD5 2009c5c675e937a0d63a8a295ff558fd
BLAKE2b-256 e8269eab77ef46a6a38144af2a79a9735aba16ad99a00bbd9d00e9b2244028a5

See more details on using hashes here.

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