[英]How can I comment out multiple cells in Jupyter Ipython / JupyterLab notebook?
How can I comment out multiple cells in Jupyter Ipython / JupyterLab notebook?如何在 Jupyter Ipython / JupyterLab notebook 中注释掉多个单元格? The code is in Python.
代码是用 Python 编写的。
The keyboard shortcuts Ctrl + / on Microsoft Windows and Cmd + / on Mac OS X only work if the selected code is within one cell. Microsoft Windows 上的键盘快捷键Ctrl + /和 Mac OS X 上的Cmd + /仅当所选代码位于一个单元格内时才有效。 However, if I select several cells, then these keyboard shortcut don't work anymore.
但是,如果我选择多个单元格,则这些键盘快捷键不再起作用。
Selecting several cells can be done by clicking on the margin of cell, then holding CTRL or SHIFT , and clicking on the margin of another cell:可以通过单击单元格的边距,然后按住CTRL或SHIFT ,然后单击另一个单元格的边距来选择多个单元格:
As stated in the comments, you can:如评论中所述,您可以:
a) Convert all them to markdown (select and press m) a) 将它们全部转换为 Markdown(选择并按 m)
b) Go cell by cell selecting all text (control+a) and then (control + /) to comment b) 逐个单元格选择所有文本 (control+a) 然后 (control + /) 进行评论
c) Merge all cells in one, and then comment. c) 将所有单元格合二为一,然后发表评论。 Unfortunately, I don't think this option is reversible
不幸的是,我不认为这个选项是可逆的
If anyone knows another option, I'd be happy to know about it!如果有人知道另一种选择,我很乐意知道!
There are two ways to solve the intention of having these cells treated as text:有两种方法可以解决将这些单元格视为文本的意图:
1) in Jupyter, multi-select the cells and press m
for markdown; 1)在Jupyter中,多选单元格,按
m
进行markdown; now they will behave like markdown/text which is just as good as commenting them out.现在它们的行为就像降价/文本一样,这和注释掉它们一样好。 Note that this process is reversible by multi-selecting the cells and pressing
y
to convert the cells to code.请注意,通过多选单元格并按
y
将单元格转换为代码,此过程是可逆的。 A drawback of this approach is the formatting that will apply by default to your markdown cells.这种方法的一个缺点是默认情况下将应用于 Markdown 单元格的格式。 To have them appear like code but behave as text seems to require the manual intervention of wrapping each markdown cell in
<pre>
tags as in: <pre>print("my code")</pre>
让它们看起来像代码但表现得像文本似乎需要手动干预,将每个 Markdown 单元格包装在
<pre>
标签中,如下所示: <pre>print("my code")</pre>
2) If there is a need to have them treated as code but commented out, the only way to do this without going one by one (that I could find), is to merge the cells into one, but that requires trickery when you have more than two cells. 2)如果需要将它们视为代码但被注释掉,唯一的方法是将单元格合并为一个,而不是一个一个地进行(我可以找到),但这需要技巧,当你有两个以上的细胞。 Once done, the command
/
(or control /
on Windows) will work on multiple lines.完成后,命令
/
(或 Windows 上的控制/
)将在多行上工作。
To do this: If only two cells involved要做到这一点:如果只涉及两个细胞
/
(windows or mac) to comment all code out/
(windows or mac) 来注释掉所有代码For more than two cells - try this:对于两个以上的单元格 - 试试这个:
/
(windows or mac)/
(windows 或 mac)As observed by others - this process is not easily reversible.正如其他人所观察到的那样 - 这个过程不容易逆转。 You can uncomment all the lines of code but splitting them back into their original cells would require manual effort I think.
您可以取消注释所有代码行,但我认为将它们拆分回原始单元格需要手动操作。
I tested all of this in Anaconda Jupyter using a sublime text editor for the text editor steps.我在 Anaconda Jupyter 中对文本编辑器步骤使用 sublime 文本编辑器测试了所有这些。 Here is the version information from my system from when I did these tests:
这是我进行这些测试时系统中的版本信息:
The version of the notebook server is 5.0.0 and is running on:
Kernel Info:
Python 3.6.1 |Anaconda custom (x86_64)| (default, May 11 2017, 13:04:09)
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.