开发者

Cell mode in Python editors

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 ce开发者_JAVA百科ll

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


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


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.


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


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


Pyscripter supports block execution. But it's Win only. And it's limited to select code block - > run it(Ctrl+F7). No notion of cells.


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.


There is Sage that offers something like this. It is meant to be a python alternative to Matlab, you should take a look.

In a sage notebook, you write python commands within blocks that are pretty similar to matlab's cell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜