1

I prefer to write my python code on VSCode because of its intellisense and autocomplete features. But I would rather see and debug the code on a Jupyter notebook to better visualize the data I am working with.

I know I can load any file into Jupyter by using the magical %load or %loadpy commands. But they load the entire file into a single cell.

Since I wanted to see the intermediary results of certain operations, I would like to import the file in such a way that each line on the file is assigned to a cell on the notebook. Unless it's a function or conditional statement (in other words, anything with indentation); in these cases it should add the whole block into one cell.

How can I do that?

FTM
  • 1,887
  • 17
  • 34
  • Any comment on my answer? – swatchai Dec 04 '18 at 04:15
  • Hi [swatchai](https://stackoverflow.com/users/2177413/swatchai), unfortunately I didn't have the time yet to take a look at your answer. I will take a look as soon as I can. – FTM Dec 05 '18 at 07:08
  • [swatchai](https://stackoverflow.com/users/2177413/swatchai), I have just tested your solution and it don't work for any code which would have indentation. – FTM Dec 05 '18 at 07:32
  • I just highlighted some of the words that implies scope of the question. ;P – swatchai Dec 07 '18 at 13:35
  • Oooops, my bad! Let me rephrase it. :) – FTM Dec 08 '18 at 19:02
  • That's not the way we do here in SO. You should ask what you just added as a new question. And reconsider what you responded to the answers that are based on the original version of your question. – swatchai Dec 10 '18 at 03:53

2 Answers2

2

AFAIK, there is no way to do this upon loading your file into a Jupyter notebook. However, you can easily split your big cell into multiple cells, by using ctrl+shift+-. This will split your cell at the position where your cursor is located at that time.

I don't know how big your files are, but it is a really fast and efficient way to split cells so maybe that works for you =)

Psychotechnopath
  • 2,471
  • 5
  • 26
  • 47
1

You can create a new Jupyter notebook file programmatically with nbformat module. Let me demonstrate all the steps with a working code.

Below are cells in a Jupyter notebook. This cell has a magic command that creates a file, named script001.py.

%%writefile script001.py
x = 10
print('x is: %s' % x)
y = 20
print('y is: %s' % y)
z = x+y
print('z is: %s' % z)

Code in the second cell below creates a new notebook, named split_cells.ipynb.

import nbformat as nbf

# create notebook object
nb2 = nbf.v4.new_notebook()

# prep cells' content for the new notebook
code = []
with open('script001.py') as fi:
    for aline in fi:
        code.append(aline.rstrip())

# take each line of code as single cells
nb2['cells'] = [nbf.v4.new_code_cell(ea) for ea in code]

# name of notebook to create
fname = 'split_cells.ipynb'

# create new notebook file
with open(fname, 'w') as fj:
    nbf.write(nb2, fj)

When you open the new notebook, split_cells.ipynb, you will have the cells like this:

In[]: x = 10

In[]: print('x is: %s' % x)

In[]: y = 20

In[]: print('y is: %s' % y)

In[]: z = x+y

In[]: print('z is: %s' % z)

This notebook is ready to run as you wish. Hope it helps.

swatchai
  • 17,400
  • 3
  • 39
  • 58
  • As I was imagining, your code works only for very limited and small amounts of code. The main reason behind that being the fact that your code don't support, for example, the creation of functions. Since it splits every single line of code, it also splits, for example, a function definition from its body. See [this print screen here](https://ibb.co/myLj84v). – FTM Dec 05 '18 at 07:31
  • Please imagine further, as this could be a starting point and not too much labour work for people who answer the question. You don't expect a complete runnable code that works with all type of your `xxx.py` files. FYI, multi-line functions can be separated into a file and imported to use with `single cell` on a Jupyter notebook. – swatchai Dec 05 '18 at 08:56
  • You are correct [swatchai](https://stackoverflow.com/users/2177413/swatchai). If your answer is improved further, it may become a real answer to my question. But, as it is not the correct answer, just pointing the right direction, I can't mark it as the correct response. But I marked it as useful. – FTM Dec 06 '18 at 12:02
  • As a matter of fact, I have full solution to your extended scope of the question. But I think it is good for another question. I don't want to edit my answer that responds to the ever-changing question. – swatchai Dec 09 '18 at 10:42