How do you convert an IPython notebook file (json with .ipynb
extension) into a regular .py
module?
From the notebook menu you can save the file directly as a python script. Go to the 'File' option of the menu, then select 'Download as' and there you would see a 'Python (.py)' option.
Another option would be to use nbconvert from the command line:
jupyter nbconvert --to script 'my-notebook.ipynb'
Have a look here.
You definitely can achieve that with nbconvert using the following command:
jupyter nbconvert --to python while.ipynb
However, having used it personally I would advise against it for several reasons:
So to summarize, there is not good way to out of the box convert python notebooks to maintainable, robust python modularized code, the only way is to manually do surgery.
In short: This command-line option converts mynotebook.ipynb
to python
code:
jupyter nbconvert mynotebook.ipynb --to python
note: this is different from above answer. ipython
has been renamed to jupyter
. the old executable name (ipython) is deprecated.
More details:
jupyter
command-line has an nbconvert
argument which helps convert notebook files (*.ipynb)
to various other formats.
You could even convert it to any one of these formats using the same command but different --to
option:
According to https://ipython.org/ipython-doc/3/notebook/nbconvert.html you are looking for the nbconvert command with the --to script option.
ipython nbconvert notebook.ipynb --to script