当 Jupyter 单元包含函数、循环或其他块时,是否可以跨单元拆分它?

2022-01-23 00:00:00 python julia jupyter-notebook

问题描述

Jupyter 具有一次执行一个单元格的功能.如果一个单元格有很多语句,则通常可以(希望)将其拆分为更小的单个语句单元格,除非涉及块,例如 if、for、def 等.

之前有人以不同的方式提出了这个问题:

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.

解决方案

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!

]1

相关文章