This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
You need pandoc, a LateX distribution, and the Notebook dataset on the book's website. On Windows, you also need pywin32 (conda install pywin32
if you use Anaconda).
data
folder. A notebook is just a plain text file (JSON), so we open it in text mode (r
mode).with open('data/test.ipynb', 'r') as f:
contents = f.read()
print(len(contents))
print(contents[:345] + '...' + contents[-33:])
json
module.import json
nb = json.loads(contents)
print(nb.keys())
print('nbformat ' + str(nb['nbformat']) +
'.' + str(nb['nbformat_minor']))
The version of the notebook format is indicated in nbformat
and nbformat_minor
.
worksheets
: there is only one by default. A worksheet contains a list of cells, and some metadata.nb['worksheets'][0].keys()
nb['worksheets'][0]['cells'][1]
nb['worksheets'][0]['cells'][2]
cells = nb['worksheets'][0]['cells']
nm = len([cell for cell in cells
if cell['cell_type'] == 'markdown'])
nc = len([cell for cell in cells
if cell['cell_type'] == 'code'])
print(("There are {nm} Markdown cells and "
"{nc} code cells.").format(
nm=nm, nc=nc))
png = cells[2]['outputs'][0]['png']
cells[2]['outputs'][0]['png'] = png[:20] + '...' + png[-20:]
cells[2]['outputs'][0]
In general, there can be zero, one, or multiple outputs. Besides, each output can have multiple representations. Here, the matplotlib figure has a PNG representation (the base64-encoded image) and a text representation (the internal representation of the figure).
jupyter
with ipython
). Here, we convert the notebook to an HTML document.!jupyter nbconvert --to html data/test.ipynb
<iframe>
(a small window showing an external HTML document within the notebook).from IPython.display import IFrame
IFrame('test.html', 600, 200)
mytemplate.tplx
that extends the default article.tplx
template provided by nbconvert. We precise the contents of the author and title blocks here.%%writefile mytemplate.tplx
((*- extends 'article.tplx' -*))
((* block author *))
\author{Cyrille Rossant}
((* endblock author *))
((* block title *))
\title{My document}
((* endblock title *))
!jupyter nbconvert --to latex --template mytemplate data/test.ipynb
!pdflatex test.tex
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).