Skip to content

Instantly share code, notes, and snippets.

@mgbckr
Last active January 31, 2022 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgbckr/038f6deacd0faa346645b973e43f7645 to your computer and use it in GitHub Desktop.
Save mgbckr/038f6deacd0faa346645b973e43f7645 to your computer and use it in GitHub Desktop.
Remove cells after execution when converting Jupyer Notebook to markdown via nbconvert by using custom templates

There is no postprocessor to exclude cells from the final output when converting Jupyter notebooks to markdown. One solution for this is to use custom templates as outlined in this gist.

Instructions:

  • Put the conf.json and index.md.j2 into a folder for example ./templates/markdown2
  • In your Jupyter notebook add the tag remove to the cells that should be removed after executing cells
  • Run the following command to execute and convert the notebook:
jupyter nbconvert notebook.ipynb --execute --to markdown --TemplateExporter.extra_template_basedirs=./templates --template markdown2 

Resources:

{
"base_template": "markdown",
"mimetypes": {
"text/markdown": true
}
}
{% extends 'markdown/index.md.j2' %}
{% block any_cell %}
{%- if "remove" not in cell.metadata.get('tags', {}) -%}
{{ super() }}
{%- endif -%}
{% endblock any_cell %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment