Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cell mode in Python editors

Tags:

In recent versions of MATLAB, one can execute a code region between two lines starting with %% using Ctrl-Enter. Such region is called a code cell, and it allows for fast code testing and debugging.

E.g.

%% This is the beginning of the 1st cell

a = 5;    

%% This is the end of the 1st cell and beginning of the 2nd cell

% This is just a comment
b = 6;

%% This is the end of the 2nd cell

Are there any python editors that support a similar feature?

EDIT: I just found that Spyderlib supports "block" execution (code regions separated with blank lines) with F9, but as the this thread mentions, this feature is still not very robust (in particular in combination with loops).

like image 668
Amelio Vazquez-Reina Avatar asked Jul 16 '11 20:07

Amelio Vazquez-Reina


People also ask

What is a cell in Python?

“Cell” objects are used to implement variables referenced by multiple scopes. For each such variable, a cell object is created to store the value; the local variables of each stack frame that references the value contains a reference to the cells from outer scopes which also use that variable.

How do I add a cell to my Spyder?

Similar to Jupyter notebook, Spyder IDE allows you to create cells. To create a cell, simply put #%% in the script. Each #%% will signal generation of a new cell. To run a cell, press shift+enter while in focus of a cell.


2 Answers

The Interactive Editor for Python IEP has a Matlab-style cell notation to mark code sections (by starting a line with '##'), and the shortcut by default is also Ctrl+Enter:

## Cell one
"""
A cell is everything between two commands starting with '##'
"""
a = 3
b = 4
print('The answer is ' + str(a+b))

## Cell two

print('Hello World')
like image 79
Amro Avatar answered Oct 28 '22 00:10

Amro


Spyder3 defines a cell as all code between lines starting with #%%.

Run a cell with Ctrl+Enter, or run a cell and advance with Shift+Enter.

like image 31
Alex Willison Avatar answered Oct 28 '22 00:10

Alex Willison