Get only the code out of Jupyter Notebook
Asked Answered
B

5

23

Is there a solution to pull out all the code of the notebook? For example, if I wanted to generate a source file of my notebook "source.py" that contained all the code in the code cells of the notebook, is that possible?

Thanks!

Blade answered 24/1, 2019 at 15:36 Comment(0)
G
35

nbconvert

You can use the command line tool nbconvert to convert the ipynb file to various other formats. The easiest way to convert it to a .py file is:

jupyter nbconvert --no-prompt --to script notebook_name.ipynb

It outputs only the code and comments without the markdown, input and output prompts. There is also --stdout option.

jq

But you can also just parse the JSON of the notebook using jq:

jq -j '
  .cells
  | map( select(.cell_type == "code") | .source + ["\n\n"] )
  | .[][]
  ' \
  notebook.ipynb > source.py
Giese answered 1/7, 2019 at 9:13 Comment(0)
C
16

You can do File -> Download as -> Python (.py) — this should export all code cells as single .py file

Candra answered 24/1, 2019 at 15:38 Comment(2)
It seems to me that now this functionality is not allowed.Premarital
@Premarital This option is only in Jupyter Classic. For Jupyter Lab (the newer replacement for Jupyter Classic), see #54350754Faletti
E
7

In case you are using jupyter lab then the option is: File > Export Notebook As > Executable Script

Echolocation answered 9/3, 2021 at 16:9 Comment(2)
Wow, that is extremely non-obvious. They should add the file extension to the option, like "Executable Script (.py)"Faletti
This is the easiest and most straightforward way!Reganregard
M
3

Since the notebook format is JSON it's relatively easy to extract just the text content of only the code cells. The task is made even easier when you use the Python API for working with notebook files.

The following will get you the code on standard output. You can handle it in other ways similarly easily. Bear in mind code source may not have a terminating newline.

from nbformat import read, NO_CONVERT

with open("Some Notebook.ipynb") as fp:
    notebook = read(fp, NO_CONVERT)
cells = notebook['cells']
code_cells = [c for c in cells if c['cell_type'] == 'code']
for cell in code_cells:
    print(cell['source'])

Notebook nodes are a little more flexible than dictionaries, though, and allow attribute (.name) access to fields as well as subscripting (['name']). As a typing-challenged person I find it preferable to write

cells = notebook.cells
code_cells = [c for c in cells if c.cell_type == 'code']

for cell in code_cells:
    print(cell.source)

In answering this question I became aware that the nbformat library has been unbundled, and can therefore be installed with pip without the rest of Jupyter.

Mayberry answered 24/1, 2019 at 16:4 Comment(0)
C
2

There is an "ugly" solution. Select all the cells of your notebook. Merge them, then just copy and paste all the code.

Colostomy answered 24/1, 2019 at 15:38 Comment(1)
Ingenious, but not scalable to multiple notebooks.Mayberry

© 2022 - 2024 — McMap. All rights reserved.