convert json ipython notebook(.ipynb) to .py file

后端 未结 10 2182
灰色年华
灰色年华 2020-12-02 12:28

How do you convert an IPython notebook file (json with .ipynb extension) into a regular .py module?

相关标签:
10条回答
  • 2020-12-02 12:49

    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.

    0 讨论(0)
  • 2020-12-02 12:49

    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:

    1. 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.
    2. 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.
    3. nbconvert still mixes execution code and library code.
    4. Comments are still not present (probably were not in the notebook).
    5. 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.

    0 讨论(0)
  • 2020-12-02 12:58

    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
    • pdf
    • python
    • rst
    • script
    • slides. (Whooh! Convert to slides for easy presentation
    0 讨论(0)
  • 2020-12-02 12:59

    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
    
    0 讨论(0)
提交回复
热议问题