问题
How do you convert an IPython notebook file (json with .ipynb
extension) into a regular .py
module?
回答1:
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.
回答2:
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
回答3:
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:
- asciidoc
- custom
- html
- latex. (Awesome if you want to paste code in conference/journal papers).
- markdown
- notebook
- python
- rst
- script
- slides. (Whooh! Convert to slides for easy presentation 😊)
the same command jupyter nbconvert --to latex mynotebook.ipynb
For more see jupyter nbconvert --help
. There are extensive options to this. You could even to execute the code first before converting, different log-level options etc.
回答4:
well first of all you need to install this packege below:
sudo apt install ipython
jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb
two option is avaliable either --to python or --to=python mine was like this works fine: jupyter nbconvert --to python while.ipynb
jupyter nbconvert --to python while.ipynb
[NbConvertApp] Converting notebook while.ipynb to python [NbConvertApp] Writing 758 bytes to while.py
pip3 install ipython
if it does not work for you try, by pip3.
pip3 install ipython
回答5:
You can use the following script to convert jupyter notebook to Python script, or view the code directly.
To do this, write the following contents into a file cat_ipynb
, then chmod +x cat_ipynb
.
#!/usr/bin/env python
import sys
import json
for file in sys.argv[1:]:
print('# file: %s' % file)
print('# vi: filetype=python')
print('')
code = json.load(open(file))
for cell in code['cells']:
if cell['cell_type'] == 'code':
print('# -------- code --------')
for line in cell['source']:
print(line, end='')
print('\n')
elif cell['cell_type'] == 'markdown':
print('# -------- markdown --------')
for line in cell['source']:
print("#", line, end='')
print('\n')
Then you can use
cat_ipynb your_notebook.ipynb > output.py
Or show it with vi
directly
cat_ipynb your_notebook.ipynb | view -
回答6:
- Go to https://jupyter.org/
- click on nbviewer
- Enter the location of your file and render it.
- Click on view as code (shown as < />)
回答7:
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:
- It's one thing to be able to convert to simple Python code and another to have all the right abstractions, classes access and methods set up. If the whole point of you converting your notebook code to Python is getting to a state where your code and notebooks are maintainable for the long run, then nbconvert alone will not suffice. The only way to do that is by manually going through the codebase.
- Notebooks inherently promote writing code which is not maintainable (https://docs.google.com/presentation/d/1n2RlMdmv1p25Xy5thJUhkKGvjtV-dkAIsUXP-AL4ffI/edit#slide=id.g3d7fe085e7_0_21). Using nbconvert on top might just prove to be a bandaid. Specific examples of where it promotes not-so-maintainable code are imports might be sprayed throughout, hard coded paths are not in one simple place to view, class abstractions might not be present, etc.
- nbconvert still mixes execution code and library code.
- Comments are still not present (probably were not in the notebook).
- There is still a lack of unit tests etc.
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.
来源:https://stackoverflow.com/questions/37797709/convert-json-ipython-notebook-ipynb-to-py-file