Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to split a Jupyter cell across cells when it contains a function, loop, or other block?

Jupyter has a feature in being able to execute one cell at a time. If a cell has a lot of statements it's often possible (desirable) to split it into smaller single statement cells, except when a block is involved,e.g if, for, def, etc.

this question was asked earlier in a different way:

Execute algorithm step by step in Jupyter

and answered

What is the right way to debug in iPython notebook?

While invoking a debugger may be the best option available, it does seem kludgy, and it likely would not work with non Python kernels.

What would be ideal is to have nested cells, and have a way to execute the entire block or the subcell.

for a Python example splitting a cell containing:

if 0 == 1:
    zero = 1
else:
    zero = 0

into, say, two cells:

if 0 == 1:
    zero = 1

and

else:
    zero = 0

likewise for Julia or R.

a debugger solution would not be my preference.

like image 273
ShpielMeister Avatar asked Dec 16 '17 00:12

ShpielMeister


People also ask

How do you split a cell into two in Jupyter Notebook?

To split the active cell at the cursor, press Ctrl + Shift + - in edit mode. You can also click and Shift + Click in the margin to the left of your cells to select them.

How do you separate text in a Jupyter Notebook?

Ctrl + Shift + - will split a cell on cursor.

What is %% Writefile in Jupyter Notebook?

Save code with %%writefile. This command lets us do the opposite of the previous command. We can save code to an external source from a cell in Jupyter Notebook using this magic command.

How do you get a cut cell in Jupyter Notebook?

If you go to "Edit", there's an option for "Undo Delete Cells". If you are familiar with shortcuts, you can do cmd + shift + p and then type in undo to recover as well.


1 Answers

Unfortunately, this is not possible. The reason for this is that the else condition by itself would cause an error. You can split the cell using control+shift+subtract but once you try and run the last cell an error occurs. You can see the exact example of this in the picture I have included. Please let me know if you have any further questions/comments!

![enter image description here]1

like image 73
Chase Kregor Avatar answered Sep 29 '22 07:09

Chase Kregor