32

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?

cqcn1991
  • 17,595
  • 41
  • 122
  • 194
  • Possible duplicate of [How to (intermittently) skip certain cells when running IPython notebook?](https://stackoverflow.com/questions/19309287/how-to-intermittently-skip-certain-cells-when-running-ipython-notebook) – Davide Fiocco May 21 '19 at 23:56

3 Answers3

43

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

Davide Fiocco
  • 4,639
  • 2
  • 34
  • 63
wgwz
  • 2,374
  • 2
  • 20
  • 31
  • 1
    Teach your friends about the trick, or add a comment in the NB explaining your method, so others could do the same. – wgwz Dec 10 '15 at 02:02
  • Wow, this is very intersting, nerver heard of it. But how can I `esc+r` for several `consecutive cells`? The code I want to turn off invovles several cells, not a singel one. – cqcn1991 Dec 10 '15 at 02:08
  • Best I got is merge the cells, and then esc+r. – wgwz Dec 10 '15 at 02:14
  • Addtionally, I tried to abstract all the cells into functions, and the trigger then by a seris of function. This can work, but make the code hard to maintain now, since the `function` and `result` are seperated in different cells. – cqcn1991 Dec 10 '15 at 07:13
  • That is super cool. Are the raw cells outputted when you convert the notebook to pdf or HTML? – BND Mar 22 '19 at 12:57
  • 1
    Figure it worth mentioning: For people that don't like keyboard shortcuts, you can also just click on the block, then use the "type" dropdown in the toolbar to switch it to "raw": https://i.imgur.com/R0Ef4W5.png – Venryx May 16 '20 at 20:37
  • thank you @Venryx I don't always like keyboard shortcuts and thought I was alone on that – naftalimich Aug 19 '20 at 21:38
26

In Jupyter notebooks one can use this magic preamble at the beginning of a cell to avoid its execution:

%%script false --no-raise-error
Davide Fiocco
  • 4,639
  • 2
  • 34
  • 63
2

You can use a condition at the cost of one extra indentation.

cellEnabled = 0
#cellEnabled = 1

if cellEnabled:
    doA()
    doB()
nvd
  • 2,598
  • 25
  • 15