Leaving a note here to adjust the solution above for cell magic (e.g. %%time), with nbconvert > 6.0.
In nbconvert_config.py:
def comment_magics(input, **kwargs):
if input.startswith("%"):
input = "# " + input
return input
# Export all the notebooks in the current directory to the sphinx_howto format
c = get_config()
c.NbConvertApp.notebooks = ["*.ipynb"]
c.Exporter.filters = {"comment_magics": comment_magics}In python_nomagic/index.py.j2:
{%- extends 'null.j2' -%}
## set to python3
{%- block header -%}
#!/usr/bin/env python3
# coding: utf-8
{% endblock header %}
## remove cell counts entirely
{% block in_prompt %}
{% if resources.global_content_filter.include_input_prompt -%}
{% endif %}
{% endblock in_prompt %}
## remove markdown cells entirely
{% block markdowncell %}
{% endblock markdowncell %}
{% block input %}
{{ cell.source | comment_magics | ipython2python }}
{% endblock input %}
In python_nomagic/conf.json:
{
"base_template": "base",
"mimetypes": {
"text/x-python": true
}
}
Convert with:
jupyter nbconvert --config ./nbconvert_config.py --to python --template ./python_nomagic notebook.ipynb