<jupyter_start><jupyter_text>Customizing nbconvert Under the hood, nbconvert uses [Jinja templates](http://jinja.pocoo.org/docs/latest/) to specify how the notebooks should be formatted. These templates can be fully customized, allowing you to use nbconvert to create notebooks in different formats with different styles as well.  Converting a notebook to an (I)Python script and printing to stdoutOut of the box, nbconvert can be used to convert notebooks to plain Python files. For example, the following command converts the `example.ipynb` notebook to Python and prints out the result:<jupyter_code>!jupyter nbconvert --to python 'example.ipynb' --stdout<jupyter_output>[NbConvertApp] Converting notebook example.ipynb to python

# coding: utf-8

# # Example notebook

# ### Markdown cells
# 
# This is an example notebook that can be converted with `nbconvert` to different formats. This is an example of a markdown cell.

# ### LaTeX Equations
# 
# Here is an equation:
# 
# $$
# y = \sin(x)
# $$

# ### Code cells

# In[1]:


print("This is a code cell that produces some output")


# ### Inline figures

# In[1]:


import matplotlib.pyplot as plt
import numpy as np
plt.ion()

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)<jupyter_text>From the code, you can see that non-code cells are also exported. If you wanted to change that behaviour, you would first look to nbconvert [configuration options page](./config_options.rst) to see if there is an option available that can give you your desired behaviour. In this case, if you wanted to remove code cells from the output, you could use the `TemplateExporter.exclude_markdown` traitlet directly, as below.<jupyter_code>!jupyter nbconvert --to python 'example.ipynb' --stdout --TemplateExporter.exclude_markdown=True<jupyter_output>[NbConvertApp] Converting notebook example.ipynb to python

# coding: utf-8

# In[1]:


print("This is a code cell that produces some output")


# In[1]:


import matplotlib.pyplot as plt
import numpy as np
plt.ion()

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)<jupyter_text>Custom Templates As mentioned above, if you want to change this behavior, you can use a custom template.  The custom template inherits from the Python template and overwrites the markdown blocks so that they are empty. Below is an example of a custom template, which we write to a file called `simplepython.tpl`. This template removes markdown cells from the output, and also changes how the execution count numbers are formatted:<jupyter_code>%%writefile simplepython.tpl

{% extends 'python.tpl'%}

## remove markdown cells
{% block markdowncell %}
{% endblock markdowncell %}

## change the appearance of execution count
{% block in_prompt %}
# [{{ cell.execution_count if cell.execution_count else ' ' }}]:
{% endblock in_prompt %}<jupyter_output>Overwriting simplepython.tpl<jupyter_text>Using this template, we see that the resulting Python code does not contain anything that was previously in a markdown cell, and only displays execution counts (i.e., `[]:` not `In[]:`):<jupyter_code>!jupyter nbconvert --to python 'example.ipynb' --stdout --template=simplepython.tpl<jupyter_output>[NbConvertApp] Converting notebook example.ipynb to python


# coding: utf-8

# [1]:

print("This is a code cell that produces some output")


# [1]:

import matplotlib.pyplot as plt
import numpy as np
plt.ion()

x = np.linspace(0, 2 * np.pi, 100)
y = np.sin(x)
plt.plot(x, y)<jupyter_text>Template structureNbconvert templates consist of a set of nested blocks. When defining a newtemplate, you extend an 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.<jupyter_code>from IPython.display import HTML, display
with open('template_structure.html') as f:
    display(HTML(f.read()))<jupyter_output><empty_output><jupyter_text>A few gotchasJinja blocks use `{% %}` by default which does not play nicely with LaTeX, so those are replaced by `((* *))` in LaTeX templates.  Templates using cell tagsThe notebook file format supports attaching arbitrary JSON metadata to each cell. In addition, every cell has a special `tags` metadata field that accepts a list of strings that indicate the cell's tags. To apply these, go to the `View → CellToolbar → Tags` option which will create a Tag editor at the top of every cell. First choose a notebook you want to convert to html, and apply the tags: `"Easy"`, `"Medium"`, or `"Hard"`.  With this in place, the notebook can be converted using a custom template.Design your template in the cells provided below.Hint: tags are located at `cell.metadata.tags`, the following Python code collects the value of the tag: ```pythoncell['metadata'].get('tags', [])```Which you can then use inside a Jinja template as in the following:<jupyter_code>%%writefile mytemplate.tpl

{% extends 'full.tpl'%}
{% block any_cell %}
{% if 'Hard' in cell['metadata'].get('tags', []) %}
    <div style="border:thin solid red">
        {{ super() }}
    </div>
{% elif 'Medium' in cell['metadata'].get('tags', []) %}
    <div style="border:thin solid orange">
        {{ super() }}
    </div>
{% elif 'Easy' in cell['metadata'].get('tags', []) %}
    <div style="border:thin solid green">
        {{ super() }}
    </div>
{% else %}
    {{ super() }}
{% endif %}
{% endblock any_cell %}<jupyter_output>Overwriting mytemplate.tpl<jupyter_text>Now, if we collect the result of using nbconvert with this template, and display the resulting html, we see the following:<jupyter_code>example = !jupyter nbconvert --to html 'example.ipynb' --template=mytemplate.tpl --stdout
example = example[3:] # have to remove the first three lines which are not proper html
from IPython.display import HTML, display
display(HTML('\n'.join(example)))<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup