jupyter - how to comment out cells?

2019-04-06 20:23发布

Is it possible to comment out whole cells in jupyter?

I need it for this case:

I have a lot of cells, and I want to run all of them, except for a few of them. I like it that my code is organized in different cells, but I don't want to go to each cell and comment out its lines. I prefer to somehow choose the cells I want to comment out, then comment them out in one go (so I could later easily uncomment them)

Thanks

3条回答
Deceive 欺骗
2楼-- · 2019-04-06 21:02

Mark the content of the cell and press Ctrl-/. It will comment out all lines in that cell. Repeat the same steps to uncomment the lines of your cell.

查看更多
The star\"
3楼-- · 2019-04-06 21:08

I think the easiest thing will be to change the cell type to 'Markdown' with M when you don't want to run it and change back to 'Code' with Y when you do. In a short test I did, I did not lose my formatting when switching back and forth.

I don't think you can select multiple cells at once.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-06 21:14

If you switch the cell to 'raw NBConvert' the code retains its formatting, while all text remains in a single font (important if you have any commented sections), so it remains readable. 'Markdown' will interpret the commented sections as headers and change the size and colour accordingly, making the cell rather messy.

On a side note I use this to interrupt the process if I want to stop it - it seems much more effective than 'Kernel --> Interrupt'.

查看更多
登录 后发表回答