Cell mode in Python editors

12,095

Solution 1

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')

Solution 2

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.

Solution 3

Spyder3 & PyCharm: #%% or # %%

Spyder3: Ctrl+Enter: to run current cell, Shift+Enter: to run current cell and advance.

PyCharm: Ctrl+Enter: to run and advance

# %%

print('You are in cell 1')

# %%

print('You are in cell 2')
# %% 

print('You are in cell 3')

enter image description here

Solution 4

IDLE with IdleX has support for Matlab-like and Sage-like cells using SubCodes. Code in between '##' markers can be executed with Ctrl+Return. It also allows for indented markers so that indented code can be executed.

Solution 5

I have written a vim plugin in which cells are delimited by ## . It sends cells to an ipython interpreter running in tmux. You can define key mappings to execute the current cell, execute current cell and move to next or execute the current line :

https://github.com/julienr/vim-cellmode

I recently started working on a similar plugin for Intellij PyCharm. It can send the cell to either the internal python console (which has some issues with plots) or to an ipython interpreter running in tmux :

https://github.com/julienr/pycharm-cellmode

Share:
12,095
Amelio Vazquez-Reina
Author by

Amelio Vazquez-Reina

I'm passionate about people, technology and research. Some of my favorite quotes: "Far better an approximate answer to the right question than an exact answer to the wrong question" -- J. Tukey, 1962. "Your title makes you a manager, your people make you a leader" -- Donna Dubinsky, quoted in "Trillion Dollar Coach", 2019.

Updated on June 06, 2022

Comments

  • Amelio Vazquez-Reina
    Amelio Vazquez-Reina almost 2 years

    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).

  • André Caron
    André Caron almost 13 years
    +1 nice find. This even appears in the features list on the front page.
  • Amro
    Amro almost 13 years
    @AndréCaron: I've used this IDE before, but never knew this feature existed until now
  • Amelio Vazquez-Reina
    Amelio Vazquez-Reina almost 13 years
    Thanks @Amro, this is the closest implementation I have seen of a MATLAB-like cell in a Python editor. Spyderlib relies on blank lines for the definition of blocks which apparently is not very robust.
  • mobeets
    mobeets almost 3 years
    For PyCharm this seems to only be supported in the paid version, but I hope I'm wrong!