Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 297
cqcn1991 Avatar asked Dec 10 '15 01:12

cqcn1991


People also ask

How do I hide certain cells in Jupyter notebook?

Hide cell inputs If you add the tag hide-input to a cell, then Jupyter Book will hide the cell but display the outputs.

How do I get rid of cut selected cells in Jupyter?

There are 2 ways to do this: Go to Edit option click on it and select "Undo Delete Cells" Use the short cut key cmd + shift + p and type undo cell deleted and click on enter to recover cell.

How do you stop a single cell in Jupyter notebook?

To interrupt a cell execution, you can click the ■ “stop” button in the ribbon above the notebook, or select “Interrupt Kernel” from the Kernel menue.

How do you cut cells in Jupyter notebook?

“delete a cell in jupyter notebook” Code Answer'sD+D(press the key twice when you are in command mode) to delete the selected cell.

How to turn off hidden code cells in iPython notebook?

When you run this code cell, by default, all the code cells will now be hidden. But you can toggle it on and off by clicking on the link. This toggle link will also be present in the published (NBViewer) version. Here is an example of an IPython notebook with this toggle link:

How do I hide the raw code in an iPython notebook?

In a code cell, add this: The raw code for this IPython notebook is by default hidden for easier reading. To toggle on/off the raw code, click <a href="javascript:code_toggle ()">here</a>.''') When you run this code cell, by default, all the code cells will now be hidden.

Is it possible to run JavaScript in an iPython notebook?

It turns out, it is possible to run javascript in the notebook if you import the HTML method from IPython: In a code cell, add this: The raw code for this IPython notebook is by default hidden for easier reading. To toggle on/off the raw code, click <a href="javascript:code_toggle ()">here</a>.''')

How to execute JavaScript in Markdown cells in iPython notebook?

The newest IPython notebook version do not allow executing javascript in markdown cells anymore, so adding a new markdown cell with the following javascript code will not work anymore to hide your code cells (refer to this link) Change ~/.ipython/profile_default/static/custom/custom.js as below:


2 Answers

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

like image 63
wgwz Avatar answered Oct 14 '22 23:10

wgwz


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

%%script false --no-raise-error 
like image 36
Davide Fiocco Avatar answered Oct 15 '22 00:10

Davide Fiocco