我用的是ipython Jupyter笔记本。假设我定义了一个函数,它在屏幕上占据了很大的空间。有办法让细胞崩溃吗?

我希望函数保持执行和可调用,但我想隐藏/折叠单元格,以便更好地可视化笔记本。我该怎么做呢?


当前回答

hide_code扩展允许您隐藏单个单元格和/或它们旁边的提示符。安装

pip3 install hide_code

访问https://github.com/kirbs-/hide_code/了解有关此扩展的更多信息。

其他回答

首先,遵循Energya的指导:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

第二是关键:打开木星笔记本后,点击nbeextension标签。现在从nbeextension提供的搜索工具中搜索“colla”(不是由web浏览器提供的),然后你会发现一些叫做“可折叠标题”的东西

这就是你想要的!

JupyterLab支持细胞折叠。单击左侧的蓝色单元格条将折叠单元格。

您可以创建一个单元格,并将以下代码放在其中:

%%html
<style>
div.input {
    display:none;
}
</style>

运行此单元格将隐藏所有输入单元格。要显示它们,可以使用菜单清除所有输出。

否则,你可以尝试笔记本扩展如下:

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/Home_3x

还有一个潘岩建议的改进版本。它添加了显示代码单元格的按钮:

%%html
<style id=hide>div.input{display:none;}</style>
<button type="button" 
onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">
Show inputs</button>

或python:

# Run me to hide code cells

from IPython.core.display import display, HTML
display(HTML(r"""<style id=hide>div.input{display:none;}</style><button type="button"onclick="var myStyle = document.getElementById('hide').sheet;myStyle.insertRule('div.input{display:inherit !important;}', 0);">Show inputs</button>"""))

我有同样的问题,我发现这个扩展有用

pip install aquirdturtle_collapsible_headings