Python 折叠 jupyter 笔记本中的单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33159518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:54:06  来源:igfitidea点击:

collapse cell in jupyter notebook

pythonipythonipython-notebookjupyter

提问by aloha

I am using ipython Jupyter notebook. Let's say I defined a function that occupies a lot of space on my screen. Is there a way to collapse the cell?

我正在使用 ipython Jupyter 笔记本。假设我定义了一个在屏幕上占据大量空间的函数。有没有办法折叠单元格?

I want the function to remain executed and callable, yet I want to hide / collapse the cell in order to better visualize the notebook. How can I do this?

我希望该函数保持执行和可调用状态,但我想隐藏/折叠单元格以便更好地可视化笔记本。我怎样才能做到这一点?

采纳答案by Energya

The jupyter contrib nbextensionsPython package contains a code-folding extension that can be enabled within the notebook. Follow the link (Github) for documentation.

jupyter contrib nbextensionsPython包包含一个代码折叠扩展,可以在笔记本内启用。按照链接 (Github) 获取文档。

To install using command line:

使用命令行安装:

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

To make life easier in managing them, I'd also recommend the jupyter nbextensions configuratorpackage. This provides an extra tab in your Notebook interface from where you can easily (de)activate all installed extensions.

为了使管理它们的生活更轻松,我还推荐该jupyter nbextensions configurator软件包。这在您的 Notebook 界面中提供了一个额外的选项卡,您可以从中轻松(取消)激活所有已安装的扩展。

Installation:

安装:

pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

回答by Pan Yan

You can create a cell and put the following code in it:

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

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

Running this cell will hide all input cells. To show them back, you can use the menu to clear all outputs.

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

Otherwise you can try notebook extensions like below:

否则你可以尝试像下面这样的笔记本扩展:

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

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

回答by Sundar

Create custom.js file inside ~/.jupyter/custom/ with following contents:

在 ~/.jupyter/custom/ 中创建 custom.js 文件,内容如下:

$("<style type='text/css'> .cell.code_cell.collapse { max-height:30px; overflow:hidden;} </style>").appendTo("head");
$('.prompt.input_prompt').on('click', function(event) {
    console.log("CLICKED", arguments)   
    var c = $(event.target.closest('.cell.code_cell'))
    if(c.hasClass('collapse')) {
        c.removeClass('collapse');
    } else {
        c.addClass('collapse');
    }
});

After saving, restart the server and refresh the notebook. You can collapse any cell by clicking on the input label (In[]).

保存后重启服务器,刷新notebook。您可以通过单击输入标签 (In[]) 来折叠任何单元格。

回答by Linas

The hide_code extension allows you to hide individual cells, and/or the prompts next to them. Install as

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

pip3 install hide_code

Visit https://github.com/kirbs-/hide_code/for more info about this extension.

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

回答by user40780

Firstly, follow Energya's instruction:

首先,按照Energya的说明:

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

Second is the key:After opening jupiter notebook, click the Nbextension tab. Now Search "colla" from the searching tool provided by Nbextension(not by the web browser), then you will find something called "Collapsible Headings"

其次是关键:打开jupiter notebook后,点击Nbextension选项卡。现在从 Nbextension 提供的搜索工具中搜索“colla”(不是通过网络浏览器),然后你会找到一个叫做“Collapsible Headings”的东西

This is what you want!

这就是你想要的!

回答by Peter Zagubisalo

There's also an improved version of Pan Yan suggestion. It adds the button that shows code cells back:

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

%%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>

Or 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>"""))

回答by Atul Singh Arora

I had a similar issue and the "nbextensions" pointed out by @Energya worked very well and effortlessly. The install instructions are straight forward (I tried with anaconda on Windows) for the notebook extensionsand for their configurator.

我有一个类似的问题,@Energya 指出的“nbextensions”工作得非常好,毫不费力。笔记本扩展它们的 configurator的安装说明很简单(我在 Windows 上尝试过使用 anaconda)。

That said, I would like to add that the following extensions should be of interest.

也就是说,我想补充一点,以下扩展应该很有趣。

  • Hide Input | This extension allows hiding of an individual codecell in a notebook. This can be achieved by clicking on the toolbar button: Hide Input

  • Collapsible Headings | Allows notebook to have collapsible sections, separated by headings Collapsible Headings

  • Codefolding | This has been mentioned but I add it for completeness Codefolding

  • 隐藏输入 | 此扩展允许在笔记本中隐藏单个代码单元。这可以通过单击工具栏按钮来实现: 隐藏输入

  • 可折叠标题 | 允许笔记本有可折叠的部分,由标题分隔 可折叠标题

  • 代码折叠 | 这已被提及,但我添加它是为了完整性 代码折叠

回答by intsco

JupyterLabsupports cell collapsing. Clicking on the blue cell bar on the left will fold the cell. enter image description here

JupyterLab支持单元格折叠。单击左侧的蓝色单元格栏将折叠单元格。 在此处输入图片说明

回答by prosti

You don't need to do much except to enable the extensions:

除了启用扩展之外,您不需要做太多事情:

http://localhost:8888/nbextensions?nbextension=collapsible_headings
http://localhost:8888/nbextensions?nbextension=codefolding/main

enter image description here

在此处输入图片说明

Most probable you will find all your extensions in here:

很可能你会在这里找到你所有的扩展:

http://localhost:8888/nbextensions

enter image description here

在此处输入图片说明

回答by Mame Gannon-Luiz

As others have mentioned, you can do this via nbextensions. I wanted to give the brief explanation of what I did, which was quick and easy:

正如其他人提到的,您可以通过 nbextensions 执行此操作。我想简要说明我所做的事情,这既快捷又简单:

To enable collabsible headings: In your terminal, enable/install Jupyter Notebook Extensions by first entering:

要启用可合并的标题:在您的终端中,首先输入以下内容来启用/安装 Jupyter Notebook Extensions:

pip install jupyter_contrib_nbextensions

Then, enter:

然后,输入:

jupyter contrib nbextension install

Re-open Jupyter Notebook. Go to "Edit" tab, and select "nbextensions config". Un-check box directly under title "Configurable nbextensions", then select "collapsible headings".

重新打开 Jupyter Notebook。转到“编辑”选项卡,然后选择“nbextensions 配置”。取消选中“Configurable nbextensions”标题下的复选框,然后选择“可折叠标题”。