43

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?

ZK Zhao
  • 19,885
  • 47
  • 132
  • 206
  • 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
  • 1
    Adding - any way to do it while saving the previous output? – YotamW Constantini Mar 19 '23 at 14:40

4 Answers4

51

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
  • 5,350
  • 5
  • 35
  • 72
wgwz
  • 2,642
  • 2
  • 23
  • 35
  • 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. – ZK Zhao 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. – ZK Zhao 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
47

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
  • 5,350
  • 5
  • 35
  • 72
3

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

cellEnabled = 0
#cellEnabled = 1

if cellEnabled:
    doA()
    doB()
nvd
  • 2,995
  • 28
  • 16
0

I had the same kind of desire and I eventually found out about the nbextension called Freeze. When you enable it, you get a nice freeze button in your toolbar. When you click it, the cell you're currently in will become "frozen". This means it will turn green (making it visually clear) and it will be ignored by the Run All process. It's also locked for editing, so you do need to unfreeze it (unlock button, two over to the left of the freeze button) before editing or running the cell. That's really easy to do though because it's just one button.

Let me know if this wasn't super clear. Otherwise, I hope this helps!

J_Code
  • 53
  • 5