Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Download from Jupyter Notebook without Extra Comments

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.

like image 625
Della Avatar asked Oct 30 '18 08:10

Della


People also ask

How do I download only the output from a Jupyter Notebook?

The Jupyter Notebook has an option to export the notebook to many formats. It can be accessed by clicking File -> Download as -> PDF via LaTeX (or PDF via HTML - not visible in the screenshot).

How do I download data from Jupyter Notebook?

Open the notebook you want to download. Click File. Click Download As. Choose a file format, then download your notebook.

How do you block comments in a Jupyter Notebook?

All you need to do, is select all the lines you want to comment and press Ctrl + / as shown in below video.

How do I export output from Jupyter?

This method is as simple as clicking File, Download as, HTML (. html). Jupyter will then download the notebook as an HTML file to wherever the browser defaults for downloaded files.


1 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

like image 91
Ammar Alyousfi Avatar answered Oct 22 '22 11:10

Ammar Alyousfi