Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Highlighted Code in Jupyter notebook Cell?

Is it possible to execute ONLY the highlighted code in a Jupyter notebook cell? This is possible in Spyder and RStudio.

I find this to be quite useful for trouble-shooting code as you write.

If a cell contains:

a=13
b=17
c=42
a=a*c

I'd like to be able to highlight and run only the desired lines (e.g. variable assignmemnts), but not the final line.

I use this frequently in Spyder and RStudio, would love to do in Jupyter as well. I find I am constantly splitting and re-combining cells in order to troubleshoot a single line of code, where for example, I've indexed into something incorrectly. Highlighting and printing the variable allows me to see what I've actually assigned it to be and is throwing an error, vs. what I had intended.

like image 765
EHB Avatar asked Oct 17 '17 18:10

EHB


People also ask

What does %% capture do Jupyter?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable. By default, %%capture discards these streams. This is a simple way to suppress unwanted output.

Which shortcut key is used to execute a cell in Jupyter?

Alt + Enter run the current cell, insert below.


1 Answers

There is no such thing in Jupyter as 'highligh and run'. At least I am not aware of it.

Run the cell after commenting the other lines out using CTRL + /, split cells and execute only the chosen ones or use a debugger (e.g. pudb, it works in Jupyter) to change variables values on the fly (while debugging).

like image 132
alex Avatar answered Sep 29 '22 18:09

alex