Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only the code out of Jupyter Notebook

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!

like image 355
J DOe Avatar asked Jan 24 '19 15:01

J DOe


People also ask

How to write Python code in Jupyter Notebook?

First, open Jupyter Notebook: it will open a new page in your browser, called Home. Start by creating a new notebook: You will immediately see the notebook name, a menu bar, a toolbar, and an empty code cell. Start by writing your first Python line code in this empty cell, and click run.

Is there a button to copy code from Jupyter notebook to clipboard?

1 It would be really handy to have a button to copy raw code from a Jupyter notebook to the clipboard so it can be dumped into a text file, .py/.R script, or straight into the terminal (a button like github has to copy to clipboard) Does a button (or even a chrome add on) exist?

How do I get keyboard shortcuts in Jupyter?

It’s worth checking this each time you update Jupyter, as more shortcuts are added all the time. Another way to access keyboard shortcuts, and a handy way to learn them is to use the command palette: Cmd + Shift + P (or Ctrl + Shift + P on Linux and Windows).

Is there a way to stop Jupyter from showing new output?

One reproducible way to force Jupyter to stop showing new output (and make the kernel hang) is to run "cat" in a cell with no arguments (on Google Chrome / Ubuntu 16.04).


2 Answers

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.

  • nbconvert documentation

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
  • jq homepage
  • Jupyter Notebook format
like image 104
Neeraz Lakkapragada Avatar answered Sep 20 '22 05:09

Neeraz Lakkapragada


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.

like image 20
holdenweb Avatar answered Sep 21 '22 05:09

holdenweb