简体   繁体   English

如何在 Jupyter Ipython / JupyterLab notebook 中注释掉多个单元格?

[英]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:可以通过单击单元格的边距,然后按住CTRLSHIFT ,然后单击另一个单元格的边距来选择多个单元格:

在此处输入图片说明

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要做到这一点:如果只涉及两个细胞

  • multi-select the cells多选单元格
  • in Jupyter menus: Edit -> Merge Cell Above (or Merge Cell Below)在 Jupyter 菜单中:编辑 -> 合并上面的单元格(或合并下面的单元格)
  • Select the contents within the whole cell选择整个单元格内的内容
  • now you can use control/command / (windows or mac) to comment all code out现在您可以使用 control/command / (windows or mac) 来注释掉所有代码

For more than two cells - try this:对于两个以上的单元格 - 试试这个:

  • multi-select all cells多选所有单元格
  • control-c or command-c (copy command) control-c 或 command-c(复制命令)
  • paste the results into your favorite text editor将结果粘贴到您喜欢的文本编辑器中
  • select all and copy the results within your text editor全选并在文本编辑器中复制结果
  • now paste back into a single cell of your notebook现在粘贴回笔记本的单个单元格中
  • now select within the notebook and use control/command / (windows or mac)现在在笔记本中选择并使用 control/command / (windows 或 mac)
  • now multi-select the rest of your cells with the original split up content现在使用原始拆分内容多选其余单元格
  • delete these cells leaving only your newly merged and commented out cell behind删除这些单元格,只留下新合并和注释掉的单元格

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM