converting from .py to .ipynb

Question:

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

The below is an example of what the code looks like now. It is a lot of code so would take hours to copy and paste it manually.

Thanks for the help.

{
   "cell_type": "code",
   "execution_count": 581,
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_trig_slope(data, size = 1, axis = 0, option = 0, random = False, lower_b = -1, upper_b = 2): n",
    "    n",
    "    # To make the gradual decline of the fuck you plotn",
    "    ## sin, cos, tan, sigmoid, other activation functions?n",
    "    # need to list the option in the doc stringn",
    "    n",
    "    ## Add a random elementn",
    "    newdata = data.copy()n",
    "    cols = list(newdata.columns)n",
    "    funcs = [math.sin, math.cos, math.tan, expit]n",
    "    func = funcs[option]n",
    "    if axis == 0:n",
    "        for col in cols:n",
    "            newdata.loc[:, col] -= size * (func(cols.index(col)))n",
    "            if random:n",
    "                newdata.loc[:,col] -= np.random.uniform(lower_b,upper_b)n",
    "n",
    "    elif axis == 1:n",
    "        for i, node in enumerate(newdata.index):n",
    "            newdata.loc[node,:] -= size * (func(i))n",
    "            if random:n",
    "                newdata.loc[node,:] -= np.random.uniform(lower_b,upper_b)n",
    "n",
    "        n",
    "    return newdatan",
    "    n",
    "    "
   ]

Asked By: SherbertTheCat

||

Answers:

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)

Answered By: Pani

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.

Answered By: Niraj Singh

ipynb-py-convert

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

Answered By: Sachin Hatikankan

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:
enter image description here

Answered By: ntg

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:

  1. Install ipynb-py-convert:

    pip install ipynb-py-convert

  2. jump to your directory level

    cd "YOUR_DIRECTORY_PATH"

  3. 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!

Answered By: YazanGhafir

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" 
Answered By: DBM