Skip to content

Commit

Permalink
Add section to customizing showing how to use template inheritance (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanv committed Mar 10, 2022
1 parent 5299ac4 commit 5d57c16
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions docs/source/customizing.rst
Expand Up @@ -140,3 +140,68 @@ from the Jupyter data directory.
For example, in the reveal template, ``index.html.j2`` extends ``base.html.j2`` which is in the same directory, and
``base.html.j2`` extends ``lab/base.html.j2``. This approach allows using content that is available in other templates
or may be overriden in the current template.

A practical example
~~~~~~~~~~~~~~~~~~~

Say you would like to modify the existing Markdown template to wrap each
output statement in a fenced code block:

.. code::
```output
(1, 2, 3)
```
Start by creating a new template directory, say ``mdoutput``. In it,
you have the following files::

conf.json
index.md.j2

The configuration file, ``conf.json`` states that your template
applies to markdown files::

{
"mimetypes": {
"text/markdown": true
}
}

The ``index.md.j2`` template entrypoint extends the existing markdown
template, and redefines how output blocks are rendered:

.. code::
{% extends 'markdown/index.md.j2' %}
{%- block traceback_line -%}
```output
{{ line.rstrip() | strip_ansi }}
```
{%- endblock traceback_line -%}
{%- block stream -%}
```output
{{ output.text.rstrip() }}
```
{%- endblock stream -%}
{%- block data_text scoped -%}
```output
{{ output.data['text/plain'].rstrip() }}
```
{%- endblock data_text -%}
You can now convert your notebook to markdown using the new template::

jupyter nbconvert --execute notebook.ipynb --to markdown --template=mdoutput

(If you put your template folder in a different location than your
notebook, remember to add
``--TemplateExporter.extra_template_basedirs=path/to/template/parent``.)

To further explore the possibilities of templating, take a look at the
root of all templates: ``null.j2``. You can find it in the
``./nbconvert/templates/base`` subfolder of one of the data paths given
by ``jupyter --paths``.

0 comments on commit 5d57c16

Please sign in to comment.