Ipython Notebook: Elegant way of turning off part of cells?

时间:2015-12-10 01:41:54

标签: python ipython-notebook

In my ipython notebook, there is part of cells that serves as preliminary inspection.

Now I want to turn it off, since after running it I know the status of the dataset, but I also want to keep it, so other people using this notebook can have this functionality.

How can I do it? Is there any example of doing it?

  1. I can comment out these cells, but then switching between on and off would be quite laborious. And may not be quite convinent for other people.

  2. I can abstract it into a function, but that itself has some methods, so the code would be quite convoluted, and may be hard to read?

3 个答案:

答案 0 :(得分:12)

Using Jupyter notebook you can click on a cell, press esc and then r. That converts it to a "raw" cell. Similar thing can be done to convert it back, esc + y. No comments needed, just key presses.

Within Juptyer notebook, go to Help -> Keyboard shortcuts for more.

Here's a snippet:

Command Mode (press Esc to enable)

  • ↩ : enter edit mode

  • ⇧↩ : run cell, select below

  • ⌃↩ : run cell

  • ⌥↩ : run cell, insert below

  • y : to code

  • m : to markdown

  • r : to raw

答案 1 :(得分:1)

在Jupyter中,一个人可以使用

%%script false

作为单元格开头的魔术前导,以避免其执行。

答案 2 :(得分:0)

您可以使用一个条件,但需要额外缩进一个。

cellEnabled = 0
#cellEnabled = 1

if cellEnabled:
    doA()
    doB()