Python 编辑器中的单元格模式

发布于 2024-11-24 20:21:18 字数 737 浏览 0 评论 0原文

在 MATLAB 的最新版本中,可以使用 Ctrl-Enter 执行以 %% 开头的两行之间的代码区域。这样的区域称为代码单元,它允许快速代码测试和调试。

例如,

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

是否有支持类似功能的 python 编辑器?

编辑:我刚刚发现Spyderlib支持“阻止”执行(代码区域以空行分隔)使用 F9,但作为此线程 提到,这个功能仍然不是很稳健(特别是与循环结合)。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

情魔剑神 2024-12-01 20:21:18

Python 的交互式编辑器 IEP 有一个 Matlab 风格的单元符号来标记代码部分(通过以'##'开始一行),默认快捷键也是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')

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')
欢烬 2024-12-01 20:21:18

Spyder3 将单元格定义为以 #%% 开头的行之间的所有代码。

使用 Ctrl+Enter 运行单元格,或者运行单元格并使用 Shift+Enter 前进。

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.

宁愿没拥抱 2024-12-01 20:21:18

Spyder3 和 Spyder3 PyCharm: #%% 或 # %%

Spyder3: Ctrl+Enter:运行当前单元格,Shift+输入:运行当前单元格并前进。

PyCharm: Ctrl+Enter:运行并前进

# %%

print('You are in cell 1')

# %%

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

print('You are in cell 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

就像说晚安 2024-12-01 20:21:18

我编写了一个 vim 插件,其中单元格由 ## 分隔。它将单元发送到在 tmux 中运行的 ipython 解释器。您可以定义键映射来执行当前单元格、执行当前单元格并移至下一个或执行当前行:

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

我最近开始为 Intellij PyCharm 开发一个类似的插件。它可以将单元格发送到内部 python 控制台(它有一些绘图问题)或发送到在 tmux 中运行的 ipython 解释器:

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

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

剪不断理还乱 2024-12-01 20:21:18

Pyscripter 支持块执行。但这仅限于Win。并且仅限于选择代码块->运行它(Ctrl+F7)。没有细胞的概念。

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.

迷路的信 2024-12-01 20:21:18

IDLE with IdleX 支持使用 SubCode 的类似 Matlab 和 Sage 的单元。 '##' 标记之间的代码可以使用 Ctrl+Return 执行。它还允许缩进标记,以便可以执行缩进代码。

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.

格子衫的從容 2024-12-01 20:21:18

Sage 提供了类似的功能。它是 Matlab 的 Python 替代品,你应该看看。

在 Sage Notebook 中,您可以在与 Matlab 单元非常相似的块中编写 Python 命令。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文