Reputation: 785
I wrote a juypter notebook that has been converted to .py somehow. I would like it back in the original format. Does anyone know how to do that?
There is a previous stack overflow question about this, but the solution doesn't work for me. Converting to (not from) ipython Notebook format
Upvotes: 57
Views: 185077
Reputation: 1377
The question was edited so this isn't as direct of an answer as it once was. Nevertheless, if you accidentally changed the extension of your Python notebook from .ipynb to .py as the OP did when originally asking the question, this is the answer for you.
Just rename it changing the extension e.g. for linux/macos
mv <file>.py <file>.ipynb
or right-click rename for windows and type the full name with the extension
(Since it seems that the contents are .ipynb contents already)
Upvotes: 12
Reputation: 31
Install jupytext package (# pip install jupytext)
import jupytext
notebook = jupytext.read('example.py')
jupytext.write(notebook, 'example.ipynb', fmt='.ipynb')
Upvotes: 3
Reputation: 91
If you wish to apply the conversion of all the .py files rooted in your project directory , you can run the following command in batch file:
for /r %%v in (*.py) do p2j "%%v"
Upvotes: 1
Reputation: 864
Renaming won't work if the py fils were py from the beginning. The easiest way might be to run the following script from PowerShell:
Install ipynb-py-convert:
pip install ipynb-py-convert
jump to your directory level
cd "YOUR_DIRECTORY_PATH"
Convert all files recursively (also in all subdirectories):
foreach ($f in Get-ChildItem "." -Filter *.py -Recurse){ ipynb-py-convert $f.FullName "$($f.FullName.Substring(0,$f.FullName.Length-3)).ipynb"}
Cheers!
Upvotes: 4
Reputation: 14075
You really should consider using jupytext
Run conda install jupytext
or pip install jupytext
Then do:
jupytext --set-formats ipynb,py <file>.ipynb
This will create the .ipynb file and for an additional bonus keep it synchronized to the .py file:
jupytext --set-formats ipynb,py <file>.ipynb --sync
This will make sure jupyter keeps the two files in sync when saving from now on...
Last note: If you are a gui person, after running the installation command for jupytext, everything else can be done from the gui as well File-->jupytext-->pair Notebook with light Script
:
Upvotes: 22
Reputation: 459
By following below steps you can get .ipynb file
Install "pip install ipynb-py-convert" Go to the directory where the py file is saved via command prompt Enter the command
ipynb-py-convert YourFileName.py YourFilename.ipynb
Eg:. ipynb-py-convert getting-started-with-kaggle-titanic-problem.py getting-started-with-kaggle-titanic-problem.ipynb
Above command will create a python script with the name "YourFileName.ipynb" and as per our example it will create getting-started-with-kaggle-titanic-problem.ipynb
file
Upvotes: 9
Reputation: 699
Use p2j to convert Python source code to Jupyter Notebook.
From the command line, run
-->pip install p2j
then go to the directory where your file is located. -->( for example-> cd downloads, if the file is in download directory)
then run
-->p2j myscript.py
This will create a myscript.ipynb file.
Upvotes: 60