Skip to main content

Build Join the chat at https://gitter.im/py4ds/nbless Coverage License PyPI PyPI - Python Version PyUp Project Status: Active – The project has reached a stable, usable state and is being actively developed.

Introduction

The nbless Python package allows you to (de)construct, convert, and execute Jupyter Notebooks in

The nbless Python package consists of 6 Python functions and shell commands:

  • nbconv, which converts a notebook into various formats.

  • nbdeck, which prepares a notebook to be viewed as or converted into slides.

  • nbexec, which runs a notebook from top to bottom and saves an executed version.

  • nbless, which calls nbuild and nbexec to create and execute a notebook.

  • nbraze, which extracts code and markdown files from a notebook.

  • nbuild, which creates a notebook from source files, e.g. Python (.py) and R (.R) scripts, markdown (.md), and text (.txt) files.

For a related package that provides programmatic tools for working with R Markdown (Rmd) files, check out the Rmdawn Python package.

Documentation and code

The documentation is hosted at https://py4ds.github.io/nbless/.

The code is hosted at https://github.com/py4ds/nbless.

Installation

pip install nbless

or clone the repo, e.g. git clone https://github.com/py4ds/nbless and install locally using setup.py (python setup.py install) or pip (pip install .).

Usage

Converting Jupyter notebooks with nbconv

The nbconv shell command can export a notebook to many different formats using the nbconvert library. Converting to all formats except HTML requires pandoc. Exporting to PDF requires LaTeX.

The supported exporters are

  • asciidoc

  • pdf

  • html

  • latex

  • markdown

  • python

  • rst

  • script

  • slides

For example, nbconv can create a python script by extracting the content from code cells and discarding all output and markdown content.

nbconv notebook.ipynb --exporter python
# Or
nbconv notebook.ipynb -e python

In the example above, the output file would be notebook.py, but you can provide a more descriptive name for the output file with the --out_file or -o flag:

nbconv notebook.ipynb --out_file script.py
# Or
nbconv notebook.ipynb -o script.py

If the exporter is not provided, nbconv will try to infer the exporter type from the out_file extension.

If neither the exporter or out_file arguments are provided, the exporter will be set to html.

nbconv notebook.ipynb

Unlike the shell command, the nbconv Python function does not create a file on its own. To create a converted file with Python, use the pathlib library.

from pathlib import Path
from nbless import nbconv

# Create notebook.py from notebook.ipynb
filename, contents = nbconv("notebook.ipynb", "python")
Path(filename).write_text(contents)

# Create report.html from notebook.ipynb
filename, contents = nbconv("notebook.ipynb", "html")
Path('report.html').write_text(contents)

Creating HTML slides with nbdeck and nbconv

With nbdeck, you can prepare HTML slides from a Jupyter notebook.

nbdeck notebook.ipynb -o slides.ipynb
nbconv slides.ipynb  -e slides -o slides.html

You can run nbdeck without nbconv, if you do not want to create HTML slides and instead want to use nbviewer or the RISE extension. If an out_file name is not provided, the notebook file contents will be printed. You can provide a more descriptive name for the executed output notebook with the --out_file or -o flag or by redirecting the output to a file with >.

nbdeck notebook.ipynb --out_file slides.ipynb
# Or
nbdeck notebook.ipynb -o slides.ipynb
# Or
nbdeck notebook.ipynb > slides.ipynb

Unlike the shell command, the nbdeck Python function does not create a file on its own. To create a converted file, use the nbformat and pathlib libraries.

import nbformat
from nbless import nbconv, nbdeck

# Create HTML slides from notebook.ipynb in notebooks folder
nbformat.write(nbdeck("notebook.ipynb"), "slides.ipynb")
filename, contents = nbconv("slides.ipynb", "slides")
Path(filename).write_text(contents)

Executing a notebook with nbexec

The nbexec command runs the input notebook from top to bottom. If an out_file name is not provided, the executed notebook contents will be printed.

nbexec notebook.ipynb

You can provide a more descriptive name for the executed output notebook with the --out_file or -o flag or by redirecting the output to a file with >.

nbexec notebook.ipynb --out_file executed.ipynb
# Or
nbexec notebook.ipynb -o executed.ipynb
# Or
nbexec notebook.ipynb > executed.ipynb

The default kernel is python3, but it is possible to specify the kernel that will be used to run notebook with the --kernel or -k flag.

nbexec notebook.ipynb --kernel ir --out_file notebook.ipynb
# Or
nbexec notebook.ipynb -k ir -o notebook.ipynb

You can preview the default output filename and the raw notebook output by running nbexec with only the positional argument:

nbexec notebook.ipynb

Unlike the shell command, the nbexec Python function does not create a file on its own. To create a notebook file, use the nbformat library.

import nbformat
from nbless import nbexec

# Create notebook.ipynb from notebook.ipynb
    nb = nbexec("notebook.ipynb")
nbformat.write(nb, "executed.ipynb")
    Rnb = nbexec("Rnotebook.ipynb")
nbformat.write(Rnb, "Rexecuted.ipynb", kernel="ir")

Creating and executing a Jupyter notebook with nbless

The nbless shell command executes a notebook created from code and markdown/text files.

nbless README.md plot.py notes.txt > executed.ipynb

The default kernel is python3, but it is possible to specify the kernel that will be used to run notebook with the --kernel or -k flag.

nbless README.md plot.py notes.txt --kernel ir > Rnotebook.ipynb
# Or
nbless README.md plot.py notes.txt -k ir > Rnotebook.ipynb

Instead of redirecting to a file (>), you can use the --out_file or -o flag:

nbless README.md plot.py notes.txt --out_file executed.ipynb
# Or
nbless README.md plot.py notes.txt -o executed.ipynb

Unlike the shell command, the nbless Python function does not create a file on its own. To create a notebook file, use the nbformat library.

import nbformat
from nbless import nbless

# Build and execute a notebook with nbless
    nb = nbless(["plot.py", "notes.txt"])
nbformat.write(nb, "executed.ipynb")
    Rnb = nbless(["plot.R", "notes.txt"], kernel="ir")
nbformat.write(Rnb, "Rexecuted.ipynb")

Extracting source files from a Jupyter notebook with nbraze

The nbraze shell command takes the contents of Jupyter Notebook code cells and turns them into code files, e.g. Python or R code files (.py or .R). The contents of markdown cells are turned into markdown files.

nbraze notebook.ipynb

The default code file extension for nbraze is py, but it is possible to set the file extension with the --extension or -e flag. If the language_info key is defined in the Jupyter notebook metadata, nbraze can try to infer the code file extension from the programming language.

nbraze notebook.ipynb --extension R
nbraze notebook.ipynb -e js
from nbless import nbraze

# Create source files from notebook.ipynb
    nbraze("notebook.ipynb")
    nbraze("notebook.ipynb", extension="R")

Creating a Jupyter notebook with nbuild

The nbuild shell command takes the contents of Python or R code files (.py or .R) and stores them as Jupyter Notebook code cells. The contents of all other files are stored in markdown cells.

nbuild README.md plot.py notes.txt > notebooks/notebook.ipynb

Instead of redirecting to a file (>), you can use the --out_file or -o flag:

nbuild README.md plot.py notes.txt --out_file notebooks/notebook.ipynb
# Or
nbuild README.md plot.py notes.txt -o notebooks/notebook.ipynb

You can preview the raw notebook output by running nbuild with only the positional arguments:

nbuild README.md plot.py notes.txt

The nbuild Python function does not create a file on its own. To create a notebook file, use the nbformat library.

import nbformat
from nbless import nbuild

# Create notebook.ipynb from plot.py and notes.txt
nb = nbuild(["plot.py", "notes.txt"])
nbformat.write(nb, "notebook.ipynb")

Next Steps

Currently, notebook metadata is lost when using nbraze/nbuild/nbless.

  • Enable nbuild/nbless to accept metadata via a metadata.json file.

  • Enable nbraze to output metadata via a metadata.json file.

Metadata

Release files for nbless 0.2.38

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nbless 0.2.38
File Size Uploaded
nbless-0.2.38.tar.gz 251.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nbless 0.2.38
File Interpreter ABI Platform
nbless-0.2.38-py3-none-any.whl Python 3 none any Details

Total release size: 264.4 kB

Release files / nbless-0.2.38.tar.gz

Download URL nbless-0.2.38.tar.gz
Size 251.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a7dcb32465ba4b17c69a11cf06924d78734dc15bfba0b74d77b86240e9d48588
BLAKE2b-256 checksum
How to use checksums
2b2163fc21557fe0bdba529ed9d261b7dde0daf3b1b46ce8c0fd2d31be7ae077
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.3

Release files / nbless-0.2.38-py3-none-any.whl

Download URL nbless-0.2.38-py3-none-any.whl
Size 12.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4c45903c708bb10615c03abcd1bd5b2098986453a4c53ba4201dcca9011ea496
BLAKE2b-256 checksum
How to use checksums
1e94ed3be2e363872bea7e491db522ae27cd050af1dca99613bd5010f3bc3a11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.2.38 This release

2 release files

0.2.37

2 release files

0.2.36

2 release files

0.2.35

2 release files

0.2.34

2 release files

0.2.26

2 release files

0.2.25

2 release files

0.2.24

2 release files

0.2.23

2 release files

0.2.22

2 release files

0.2.21

2 release files

0.2.20

2 release files

0.2.19

2 release files

0.2.18

2 release files

0.2.9

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

3 release files

0.1.0

2 release files

0.0.16

1 release file

0.0.15

3 release files

0.0.14

3 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page