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 notebook would be printed to the screen (stdout), but you can
create or overwrite a notebook files 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.
frompathlibimportPathfromnblessimportnbconv# Create notebook.py from notebook.ipynbfilename, contents=nbconv("notebook.ipynb", "python")
Path(filename).write_text(contents)
# Create report.html from notebook.ipynbfilename, 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.
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.
importnbformatfromnblessimportnbconv, nbdeck# Create HTML slides from notebook.ipynb in notebooks foldernbformat.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 to the screen (stdout). This can be useful for previewing the output.
nbexec notebook.ipynb
You can create or overwrite a notebook file 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
as in the example with the IRkernel below.
nbexec notebook.ipynb --kernel ir --out_file notebook.ipynb
# Or
nbexec notebook.ipynb -k ir -o 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.
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.
importnbformatfromnblessimportnbless# Build and execute a notebook with nblessnb=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
fromnblessimportnbraze# Create source files from notebook.ipynbnbraze("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.
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.
importnbformatfromnblessimportnbuild# Create notebook.ipynb from plot.py and notes.txtnb=nbuild(["plot.py", "notes.txt"])
nbformat.write(nb, "notebook.ipynb")
The packages listed above can all convert Jupyter notebooks to other formats:
markdown files (all three) or
Python scripts (jupytext).
Nbless wraps jupyter nbconvert to convert notebooks to other file types, but it can do something all of the aforementioned packages cannot.
Nbless can take a more modular approach to file conversion by extracting the contents of each notebook cell into a separate file (cell -> file) or using a source file to create each notebook cell (file -> cell).
Looking beyond simple file conversion, Nbless includes a tool for making slides from notebooks (by setting slide_type in notebook metadata).
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.
请发表评论