Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a IPython Notebook into a Python file via commandline?

I'm looking at using the *.ipynb files as the source of truth and programmatically 'compiling' them into .py files for scheduled jobs/tasks.

The only way I understand to do this is via the GUI. Is there a way to do it via command line?

like image 313
Stefan Krawczyk Avatar asked Jun 13 '13 00:06

Stefan Krawczyk


People also ask

How do I convert a python notebook to python?

If you want to convert your single notebook to an executable Python script (. py), you can easily do this task using the 'nbconvert' package. For example, we have created a file named 'testnotebook. ipynb', and we want to convert this file to python (.

How do I export a python 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 a python file from Jupyter Notebook?

You can download individual files from the file navigator (which you can get to by clicking on the Jupyter icon in the top left corner). You just need to click the checkbox next to the file you want to download and then click the "download" button at the top of the pane.


12 Answers

If you don't want to output a Python script every time you save, or you don't want to restart the IPython kernel:

On the command line, you can use nbconvert:

$ jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb

As a bit of a hack, you can even call the above command in an IPython notebook by pre-pending ! (used for any command line argument). Inside a notebook:

!jupyter nbconvert --to script config_template.ipynb

Before --to script was added, the option was --to python or --to=python, but it was renamed in the move toward a language-agnostic notebook system.

like image 200
wwwilliam Avatar answered Oct 07 '22 09:10

wwwilliam


If you want to convert all *.ipynb files from current directory to python script, you can run the command like this:

jupyter nbconvert --to script *.ipynb
like image 40
Břetislav Hájek Avatar answered Oct 07 '22 10:10

Břetislav Hájek


Here is a quick and dirty way to extract the code from V3 or V4 ipynb without using ipython. It does not check cell types, etc.

import sys,json

f = open(sys.argv[1], 'r') #input.ipynb
j = json.load(f)
of = open(sys.argv[2], 'w') #output.py
if j["nbformat"] >=4:
        for i,cell in enumerate(j["cells"]):
                of.write("#cell "+str(i)+"\n")
                for line in cell["source"]:
                        of.write(line)
                of.write('\n\n')
else:
        for i,cell in enumerate(j["worksheets"][0]["cells"]):
                of.write("#cell "+str(i)+"\n")
                for line in cell["input"]:
                        of.write(line)
                of.write('\n\n')

of.close()
like image 21
Valentas Avatar answered Oct 07 '22 11:10

Valentas


Following the previous example but with the new nbformat lib version :

import nbformat
from nbconvert import PythonExporter

def convertNotebook(notebookPath, modulePath):

  with open(notebookPath) as fh:
    nb = nbformat.reads(fh.read(), nbformat.NO_CONVERT)

  exporter = PythonExporter()
  source, meta = exporter.from_notebook_node(nb)

  with open(modulePath, 'w+') as fh:
    fh.writelines(source.encode('utf-8'))
like image 34
Spawnrider Avatar answered Oct 07 '22 11:10

Spawnrider


Jupytext is nice to have in your toolchain for such conversions. It allows not only conversion from a notebook to a script, but you can go back again from the script to notebook as well. And even have that notebook produced in executed form.

jupytext --to py notebook.ipynb                 # convert notebook.ipynb to a .py file
jupytext --to notebook notebook.py              # convert notebook.py to an .ipynb file with no outputs
jupytext --to notebook --execute notebook.py    # convert notebook.py to an .ipynb file and run it 
like image 37
Wayne Avatar answered Oct 07 '22 09:10

Wayne


You can do this from the IPython API.

from IPython.nbformat import current as nbformat
from IPython.nbconvert import PythonExporter

filepath = 'path/to/my_notebook.ipynb'
export_path = 'path/to/my_notebook.py'

with open(filepath) as fh:
    nb = nbformat.reads_json(fh.read())

exporter = PythonExporter()

# source is a tuple of python source code
# meta contains metadata
source, meta = exporter.from_notebook_node(nb)

with open(export_path, 'w+') as fh:
    fh.writelines(source)
like image 21
justanr Avatar answered Oct 07 '22 11:10

justanr


I understand this is an old thread. I have faced the same issue and wanted to convert the .pynb file to .py file via command line.

My search took me to ipynb-py-convert

By following below steps I was able to get .py file

  1. Install "pip install ipynb-py-convert"
  2. Go to the directory where the ipynb file is saved via command prompt
  3. Enter the command

> ipynb-py-convert YourFileName.ipynb YourFilename.py

Eg:. ipynb-py-convert getting-started-with-kaggle-titanic-problem.ipynb getting-started-with-kaggle-titanic-problem.py

Above command will create a python script with the name "YourFileName.py" and as per our example it will create getting-started-with-kaggle-titanic-problem.py file

like image 26
Sachin Hatikankan Avatar answered Oct 07 '22 11:10

Sachin Hatikankan


The following example turns an Iron Python Notebook called a_notebook.ipynb into a python script called a_python_script.py leaving out the cells tagged with the keyword remove, which I add manually to the cells that I don't want to end up in the script, leaving out visualizations and other steps that once I am done with the notebook I don't need to be executed by the script.

import nbformat as nbf
from nbconvert.exporters import PythonExporter
from nbconvert.preprocessors import TagRemovePreprocessor

with open("a_notebook.ipynb", 'r', encoding='utf-8') as f:
    the_notebook_nodes = nbf.read(f, as_version = 4)

trp = TagRemovePreprocessor()

trp.remove_cell_tags = ("remove",)

pexp = PythonExporter()

pexp.register_preprocessor(trp, enabled= True)

the_python_script, meta = pexp.from_notebook_node(the_notebook_nodes)

with open("a_python_script.py", 'w', encoding='utf-8') as f:
    f.writelines(the_python_script)
like image 28
Pablo Adames Avatar answered Oct 07 '22 09:10

Pablo Adames


For converting all *.ipynb format files in current directory to python scripts recursively:

for i in *.ipynb **/*.ipynb; do 
    echo "$i"
    jupyter nbconvert  "$i" "$i"
done
like image 44
Don Smythe Avatar answered Oct 07 '22 10:10

Don Smythe


Using nbconvert 6.07 and jupyter client 6.1.12:

Convert jupyter notebook to python script

$ jupyter nbconvert mynotebook.ipynb --to python

Convert jupyter notebook to python script specifying output filename

$ jupyter nbconvert mynotebook.ipnb --to python --output myscript.py
like image 21
yaach Avatar answered Oct 07 '22 10:10

yaach


There's a very nice package called nb_dev which is designed for authoring Python packages in Jupyter Notebooks. Like nbconvert, it can turn a notebook into a .py file, but it is more flexible and powerful because it has a lot of nice additional authoring features to help you develop tests, documentation, and register packages on PyPI. It was developed by the fast.ai folks.

It has a bit of a learning curve, but the documentation is good and it is not difficult overall.

like image 2
John Avatar answered Oct 07 '22 10:10

John


no file/directory error

On my mint [ubuntu] system at work, even though jupyter was already installed and notebooks worked, jupyter nbconvert --to script gave the error no file/directory until I did a separate

sudo apt-get install jupyter-nbconvert

Then all was fine with the conversion. I just wanted to add this in case anyone hits the same error (for me it was confusing as I thought the no file error referred to the notebook, which was definitely there in the local directory, took me a while to realize the subcommand was not installed).

like image 1
Adrian Tompkins Avatar answered Oct 07 '22 11:10

Adrian Tompkins