1

I have converted IPYNB format to PY format. It looks some what similar to that of jupyter notebook. [with cell number etc.]

E.g.: 
#!/usr/bin/env python
# coding: utf-8

# In[19]:
e = 4 

# In[25]:
if 'x' == 10
  • if i convert this "PY format" back to "IPYNB format", i am getting entire program in the same cell, although we have cell numbers to differentiate
  • Is there anyway to convert "PY format" back to "IPYNB format", with different cells, because I don't have the IPYNB files.
AdityaMani
  • 45
  • 1
  • 5
  • https://pypi.org/project/ipynb-py-convert/ looks like it can do that – Stef Sep 06 '22 at 07:58
  • Related: https://stackoverflow.com/questions/23606659/is-there-a-way-to-create-a-ipynb-from-a-py-file-command-line https://stackoverflow.com/questions/62510114/converting-from-py-to-ipynb – Stef Sep 06 '22 at 07:59
  • *"I have converted IPYNB format to PY format."* What tool did you use to do that? Supposedly, if someone wrote a tool to convert from ipynb to py, they also wrote a corresponding tool for the inverse operation. But since the `# In[25]:` comments in the py file are relatively non-standard, if you wrote your own ipynb-to-py tool, you likely need to also write your own py-to-ipynb tool, as a different tool might use a different notation. – Stef Sep 06 '22 at 08:01
  • I haven't used any tool its just direct "Download --> as Py " – AdityaMani Sep 06 '22 at 15:15
  • i have tried the "ipynb-py-convert", its giving everything in the same cell, cant i get different cells for each "In[]" – AdityaMani Sep 06 '22 at 15:19
  • Yes, you can get different cells. But ipynb-py-convert has its own format. Your py file separates the different cells with a simple `# In[19]:` comment; Ipynb-py-convert also separates the different cells somehow, but not exactly like that. So that's why it doesn't recognise different cells in your file. – Stef Sep 06 '22 at 15:21
  • After just a very-quick glance at ipynb-py-convert, I suggest replacing `# In[19]:` with `# %%` in your py file. – Stef Sep 06 '22 at 15:23

1 Answers1

1

Quick and dirty option

Your example works easily using Jupytext.

In a terminal in your Jupyter session, where I saved your code example as test_dl_script.py :

pip install jupytext
jupytext --to notebook test_dl_script.py 

That gives threee notebooks cells with your two code lines separate.

For more complex things, it won't be perfect and would take some editing to the notebooks created.

In the future you are better off keeping the notebook files and using Jupytext when you want to convert. It goes back round trip correctly when it is used all the way through. See here.


More custom option

If you have way too many to deal with the imperfections you are going to see trying Jupytext and your notebook-derived code is very consistent with how markdown vs code cells is demarcated, you could custom script using nbformat to do this. I have posted several examples using nbformat with code and markdown cells in the Jupyter Discrouse Forum that can be found here. Plus, there's several code blocks I have here at Stackoverflow using nbformat. (If you contact me via the Jupyter Discourse forum, I'd be more than happy to help you adapt nbformat to deal with the script code you have now to get it back to a notebook.)

Wayne
  • 6,607
  • 8
  • 36
  • 93