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

依然范特西╮ 提交于 2020-03-17 10:22:05

问题


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?


回答1:


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




回答2:


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

%%script false

These added arguments will prevent the notebook from erroring:

%%script false --no-raise-error


来源:https://stackoverflow.com/questions/34192290/ipython-notebook-elegant-way-of-turning-off-part-of-cells

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!