38
nbconvert Documentation, Release 5.0.0.dev
# This was input cell with execution count: {{ cell.execution_count if cell.execution_count else ' ' }}
{%- endblock in_prompt %}
Overwriting simplepython.tpl
Usingthis template,wesee thattheresultingPython codedoesnotcontainanythingthatwas previouslyina markdown
cell, and has special comments regarding the execution counts:
In [3]: !jupyter nbconvert --to python 'example.ipynb' --stdout --template=simplepython.tpl
# coding: utf-8
# This was input cell with execution count: 1
print("This is a code cell that produces some output")
# This was input cell with execution count: 2
get_ipython().magic('matplotlib inline')
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2
*
np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
[NbConvertApp] Converting notebook example.ipynb to python
7.1 Template structure
Nbconvert templates consist of a set ofnested blocks. When defininga new template, you extendan existing template
by overriding some of the blocks.
All the templates shipped in nbconvert have the basic structure described here, though some may define additional
blocks.
In [4]: from IPython.display import HTML, display
with open('template_structure.html') as f:
display(HTML(f.read()))
7.1.1 A few gotchas
Jinja blocks use {% %} by default which does not play nicely with LaTeX, so those are replaced by ((
* *
)) in
LaTeX templates.
7.2 Templates that use cell metadata
The notebook file format supports attaching arbitrary JSON metadata to each cell. Here, as an exercise, you will use
the metadata totag cells.
28
Chapter 7. Customizing nbconvert