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!
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.
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?
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).
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).
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With