Jupyter Notebook: command for hide the output of a cell?

前端 未结 7 704
被撕碎了的回忆
被撕碎了的回忆 2021-02-03 17:30

In my notebook, I have a cell returning temp calculation results. It\'s a bit long, so after it is run, I want to hide it and when needed, to show it.

To do it manually,

相关标签:
7条回答
  • 2021-02-03 17:45

    If you don't mind a little hacking, then you may write a simple script for inverting the "collapsed" attribute of each cell from false to true in the notebook .ipynb file (which is a simple JSON file). This is however may fail in the future if a the .ipynb format changes.

    0 讨论(0)
  • 2021-02-03 17:49

    In the newer versions(5.0.0 at the time I'm writing this), pressing o in the command mode hides the output of the cell in focus. The same happens if you triple click in front of the output.

    o is

    • the first letter in the word output or
    • lower case of 15th letter in the alphabet
    0 讨论(0)
  • 2021-02-03 17:49

    In newer versions of Jupiter Notebook, select the desired cell, make sure you're in command mode and then on the menubar press Cell > Current Outputs. You have then three options:

    • Toggle (press O in the command mode to apply the same effect)
    • Toggle Scrolling (the default output)
    • Clear (to clear the output all together)

    Image to Menubar Options

    Additionally, you can apply the same effect to all the cells in your document if you chose All Output instead of Current Output.

    0 讨论(0)
  • 2021-02-03 17:55

    Based on this, I just came up with this for myself a few minutes ago:

    %%javascript
    
    $('#maintoolbar-container').children('#toggleButton').remove()
    
    var toggle_button = ("<button id='toggleButton' type='button'>Show Code</button>");
    $('#maintoolbar-container').append(toggle_button);
    
    var code_shown = false;
    
    function code_toggle()
    {
    
        if (code_shown)
        {
            console.log("code shown")
            $('div.input').hide('500');
            $('#toggleButton').text('Show Code');
        }
        else
        {
            console.log("code not shown")
            $('div.input').show('500');
            $('#toggleButton').text('Hide Code');
        }
    
        code_shown = !code_shown;
    }
    
    $(document).ready(function()
    {
        code_shown=false;
        $('div.input').hide();
    });
    
    $('#toggleButton').on('click', code_toggle);
    

    It does have a glitch: each time you run that cell (which I put at the top), it adds a button. So, that is something that needs to be fixed. Would need to check in the maintoolbar-container to see if the button already exists, and then not add it.

    EDIT

    I added the necessary piece of code:

    $('#maintoolbar-container').children('#toggleButton').remove()
    
    0 讨论(0)
  • 2021-02-03 18:00

    Not exactly what you are after, but the effect might be good enough for your purposes:

    Look into the %%capture magic (https://nbviewer.jupyter.org/github/ipython/ipython/blob/1.x/examples/notebooks/Cell%20Magics.ipynb). It lets you assign that cell output to a variable. By calling that variable later you could see the output.

    0 讨论(0)
  • 2021-02-03 18:01

    Add ; by the end of the cell to hide the output of that cell.

    0 讨论(0)
提交回复
热议问题