2

My organisation converts any Jupyter Notebooks (.ipynb files) it makes into python scripts (.py files) for easier management in our repos. I need to convert them back so I can run the notebooks but can't work out how.

I believe they've been encoded using the nbconvert package but I couldn't find a way to convert the files back in the package docs.

I've included the head of one of the .py files bellow in case it makes the encoding format more obvious.

# ---
# jupyter:
#   jupytext:
#     formats: ipynb,py:light
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.4'
#       jupytext_version: 1.2.0
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Title/filename here

# ### Initialise

import pandas as pd
import numpy as np
# etc


# ### Title here

# + 
do_stuff()



j-hil
  • 115
  • 10
  • Does this help https://stackoverflow.com/questions/37797709/convert-json-ipython-notebook-ipynb-to-py-file –  May 03 '22 at 10:57
  • I believe that's the other way around (.ipynb -> .py) but I need .py -> .ipynb. Thanks – j-hil May 03 '22 at 11:00
  • Alright I read your question opposite I'll keep looking –  May 03 '22 at 11:01
  • ```pip install p2j``` the ```p2j myfile.py``` could work? I'd try to make sure the encoding matches –  May 03 '22 at 11:05
  • Thanks @MATOS but looks like it wasn't encoded using p2j, and I don't have admin rights to install it anyhow. I think I've answered my own question though now. – j-hil May 03 '22 at 13:32

1 Answers1

2

Looks like the file was actually converted using program jupytext.

To convert it back I found running jupytext --to ipynb my_file.py worked.

More simply: opening the .py file in jupyter notebook allowed me to view the .py file as if it were a .ipynb file without converting.

j-hil
  • 115
  • 10
  • "More simply: opening the .py file in jupyter notebook allowed me to view the .py file as if it were a .ipynb file without converting." Sneaky. I will try this. Thanks – Nesha25 Nov 22 '22 at 20:18