11

Is there a way to create a .ipynb from a .py file command line?

Why I would want to do this: I want to refactore some code using a text editor. My ipynb file is on a server, and it is way easier to transfer a .py file (generated by running ipython notebook with --script) that is tracked on a version control system (git) than ipynb.

Ideally I want to do the following:

  • the Notebook is running with --script, so I have a .py file
  • commit the new version of the file, push from the server and pull on my machine
  • pull from my machine
  • edit on my machine
  • commit, push from my machine and pull from the server
  • create the .ipynb file from the new .py file and replace the old one

And I can't find any way to start on the server an iPython Notebook from a py file that is on the server.

Sure, I can add the .ipynb to git, but there are good reasons not to do that. I could also send files using ftps, but since it is something I do rather often I'd like to find a better solution.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
maxbellec
  • 16,093
  • 10
  • 36
  • 43
  • 1
    possible duplicate of [Converting to (not from) ipython Notebook format](http://stackoverflow.com/questions/23292242) – Bhargav Rao Jul 05 '16 at 11:19

2 Answers2

9

I believe it is covered here: https://stackoverflow.com/a/23292713/3025981. I reproduce the snipped here:

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')
Community
  • 1
  • 1
Ilya V. Schurov
  • 7,687
  • 2
  • 40
  • 78
  • 2
    @BhargavRao, actually, I can just copy-paste the answer from the link, but I think that this question can be also marked as a dupe of the question I refer to. However, I'm not sure... – Ilya V. Schurov Dec 02 '14 at 13:00
4

From https://pypi.org/project/ipynb-py-convert/

pip install ipynb-py-convert

Example

ipynb-py-convert examples/plot.py examples/plot.ipynb

or

ipynb-py-convert examples/plot.ipynb examples/plot.py
Mark K
  • 8,767
  • 14
  • 58
  • 118