How to Download from Jupyter Notebook without Extra Comments

Viewed 3293

Apologies if it has already been asked, or if the solution is trivially simple.

Using Jupyter Notebook for Python scripting. When I download a notebook as a .py file (by clicking on File->Download as->Python (.py)), Jupyter adds a bunch of extraneous commented lines. It adds some interpreter shebang, encoding declaration at the beginning, and then #In[], #Out[] for each cell etc. To make matters more troublesome, the shebang looks like

#!/usr/bin/env python

even though I am using a python3 kernel.

While I am sure it has the best intentions, very often I want to have my own interpreter directive and skip the other commented lines altogether since they do nothing but adding to the clutter.

How to download just the raw codes, only with comments that I inserted, and cells separated by nothing but line breaks? Also, I would like to know a permanent solution to change the configuration to download it this way, for all ipynb files on my machine, not a one time commmand.

3 Answers

You can use this command:

jupyter nbconvert --to python "path/to/notebook.ipynb" \
--TemplateExporter.exclude_markdown=True \
--TemplateExporter.exclude_output_prompt=True \
--TemplateExporter.exclude_input_prompt=True

It will produce a Python file containing only your Python code and without #In[], #Out[]

The \ in the command is just to write the command on multiple lines and can be removed.

Permanent Configuration

To make this configuration permanent:

  1. Create a file named jupyter_nbconvert_config.py inside the directory ~/.jupyter/. Create the directory if not existing.

  2. Write inside the file:

    from nbconvert import TemplateExporter
    TemplateExporter.exclude_markdown=True 
    TemplateExporter.exclude_output_prompt=True
    TemplateExporter.exclude_input_prompt=True
    
  3. Now you can run the command:

    jupyter nbconvert --to python "path/to/notebook.ipynb"
    

    to get a Python file with code only.

Sources: 1 2

A late response, but you can directly change the python.tpl file which refers to the template Jupyter Notebook uses when creating your python file.

  1. Find your python.tpl file. Mine is located at: C:\Anaconda3\envs\<my_env_name>\Lib\site-packages\nbconvert\templates, where the <my_env_name> is just the name of an environment I created. I just did a windows search on 'nbconvert\templates' to find the directory.
  2. Back up the original python.tpl file by copying and pasting it somewhere. I just made an original_templates folder in the same directory above.
  3. Edit the python.tpl file (not the one you copied for backup) with your desired changes to the template. If you just want the code blocks without the headers / comments, below is the basic structure I used:
{%- extends 'null.tpl' -%} 
{% block input %} 
{{ cell.source | ipython2python }} 
{% endblock input %}
  1. Save changes.

Now all future downloads as a python file in this environment should just be the code without the extra comments.

For more details on customizing the template, try referring to the nbconvert docs: https://nbconvert.readthedocs.io/en/latest/customizing.html

Note: I edited the template file in the environment, so if I switch environments within anaconda I would also need to make the sames changes to the template file in the other environment.

Not exactly a fix, but you know how it is, sometimes you just gotta get the job done.

I downloaded my notebook with

jupyter nbconvert --to python my/path/notebook.ipynb

Then, I just opened it with Sublime Text.

After that, just needed to get rid of all the cell references e.g. : '# In[22]:' in the script. Using find and replace in Sublime Text you can select RegEx, and this:

(# In\W[0-9]+\W:)+

Will solve that.

Related