collapse cell in jupyter notebook

前端 未结 12 1291
长发绾君心
长发绾君心 2020-12-22 17:00

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?

I want the func

相关标签:
12条回答
  • 2020-12-22 17:02

    Firstly, follow Energya's instruction:

    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"

    This is what you want!

    0 讨论(0)
  • 2020-12-22 17:05

    JupyterLab supports cell collapsing. Clicking on the blue cell bar on the left will fold the cell.

    0 讨论(0)
  • 2020-12-22 17:05

    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

    0 讨论(0)
  • 2020-12-22 17:09

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

    pip3 install hide_code
    

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

    0 讨论(0)
  • 2020-12-22 17:10

    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:

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

    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".

    0 讨论(0)
  • 2020-12-22 17:14

    What I use to get the desired outcome is:

    1. Save the below code block in a file named toggle_cell.py in the same directory as of your notebook
    from IPython.core.display import display, HTML
    toggle_code_str = '''
    <form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Sloution"></form>
    '''
    
    toggle_code_prepare_str = '''
        <script>
        function code_toggle() {
            if ($('div.cell.code_cell.rendered.selected div.input').css('display')!='none'){
                $('div.cell.code_cell.rendered.selected div.input').hide();
            } else {
                $('div.cell.code_cell.rendered.selected div.input').show();
            }
        }
        </script>
    
    '''
    
    display(HTML(toggle_code_prepare_str + toggle_code_str))
    
    def hide_sloution():
        display(HTML(toggle_code_str))
    
    1. Add the following in the first cell of your notebook
    from toggle_cell import toggle_code as hide_sloution
    
    1. Any cell you need to add the toggle button to simply call hide_sloution()
    0 讨论(0)
提交回复
热议问题