vscode-jupyter icon indicating copy to clipboard operation
vscode-jupyter copied to clipboard

Allow for nested Jupyter code cells

Open PALuczak opened this issue 4 years ago • 5 comments
trafficstars

It would be very useful to be able to nest Jupyter code calls based on indentation. This would make it possible to run for example just a part of a loop. For example:

# %% < -- top-level cell
j = 0
for i in range(10):
    # %% < -- sub cell 1
    print(i)
    # %% < -- sub cell 2
    for k in range(10):
        # %% < -- sub sub cell 1
        print(i * i)
    # %% < -- sub cell 3
    j += i
# %%  < -- another top-level cell
print(j)

in this scenario triggering the outer cell would run the entire loop, while the inner cell would only run the code inside the loop. This would be especially useful during data cleaning, where the processing step needs to be fine-tuned before being run on the whole dataset. If it makes the implementation more convenient, the inner cells could also be designated by adding additional percent characters for each nesting level, i.e.: # %% - top-level cell, # %%% - subcell, # %%%% - subsubcell etc.

PALuczak avatar Mar 05 '21 10:03 PALuczak

Hi @PALuczak, if you select code with your mouse you can right click and select the option "Run Selection/Line in Interactive Window". You can even adjust the keyboard shortcut for this if you would like a different one. Does that solution work for you?

claudiaregio avatar Mar 06 '21 00:03 claudiaregio

That is more of a workaround than a solution. Selection is quite error-prone and oftentimes it leads to selecting either one too many or one too few lines. The proposed subcells would significantly reduce such errors.

Furthermore, this additional structure would be very useful in teaching, as we would be able to show the processing step-by-step before running it on the whole dataset without having to duplicate code or having to ensure that they select the exact line numbers that describe a particular step.

PALuczak avatar Mar 08 '21 09:03 PALuczak

Hi @claudiaregio, I agree with @PALuczak. The problem with here is UI ergonomics and usability. In your approach you have to use a mouse, select a part of your code, right click, select an option. Or, with keyboard shortcuts - select a part of code (with shift and arrows) and then press the Run Selection shortcut. In contrary to this a single shortcut (e.g. Ctrl+Enter) in a subcell sounds quite effective. Moreover, such feature would be very helpful during code tuning/debugging sessions - you make a small change and execute it to see the result asap.

tomekjaworski avatar Mar 08 '21 09:03 tomekjaworski

Another approach to this is the proposal to auto-detect code blocks around the cursor, with Shift+Enter executing the whole block: https://github.com/microsoft/vscode-jupyter/issues/1471 Then you could run the whole for loop when the cursor is on the for line. It wouldn't give exactly the same functionality but there is some overlap.

andycraig avatar Mar 10 '21 22:03 andycraig

While there is some slight overlap with #1471, the approach proposed here is significantly more flexible as it would allow for more incremental splits. Oftentimes, either the first or the last step in the loop is costly (i.e. getting/saving data) so being able to split it from the other steps would be quite useful.

Additionally, as mentioned in my previous comment, being able to specify blocks manually would be very useful in education as it would allow for creating step-by-step tutorials for students.

PALuczak avatar Mar 12 '21 08:03 PALuczak

thanks for the suggestion, unfortunately I don't think there is enough interest for us to prioritize this feature

amunger avatar Dec 07 '22 20:12 amunger