How to hide one specific cell (input or output) in IPython Notebook?

后端 未结 10 1474
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 21:53

Is there a way to selectively hide one specific input or output cell in IPython notebook?

I could only find the below code to show / hide all input cells.

h

相关标签:
10条回答
  • 2020-11-29 22:19

    Finally found it's possible using this extension.

    https://github.com/ipython-contrib/IPython-notebook-extensions/blob/master/usability/hide_input.js

    0 讨论(0)
  • 2020-11-29 22:27

    Regarding the output, in Jupiter notebook, there is also an option on the bar: You can Clear the output or you can hide it using Toggle. In both cases, you won't delete any variable calculated inside the cell.

    0 讨论(0)
  • 2020-11-29 22:27

    The @Mathmagician solution is almost perfect, but has many side effects.

    More correct would be like:

    from IPython.core.display import display, HTML
    toggle_code_str = '''
    <form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Toggle Code"></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 toggle_code():
        display(HTML(toggle_code_str))
    

    The call toggle_code than may be placed in some code cell before other code, so if code in the cell is executed slowly, won't be side effects. Also it solves the problem with Run Cells and Select/Insert Below

    It adds the toggle button, but the initial state can't be managed

    0 讨论(0)
  • 2020-11-29 22:28

    Here's a method that allows you to hide cells from the HTML/PDF output by editing the cell metadata only.

    Versions I'm using:

    $ jupyter notebook --version

    4.1.0

    $ jupyter nbconvert --version

    4.2.0

    1. Download the ipython notebook extension templates by following install instructions on Github: pip install https://github.com/ipython-contrib/IPython-notebook-extensions/tarball/master
      • This just install files into your local jupyter data directory. Full details in the readme
    2. run jupyter notebook
    3. go to localhost:8888/nbextensions (or whatever port you started on) and activate Printview
    4. go back to localhost:8888/tree, create a new notebook and go into it
    5. create a code cell with some code in it that produces output e.g. print("You can see me") #but not me
    6. go to View > Cell Toolbar > Edit Metadata
    7. click the Edit Metadata button now showing to the top right of the cell
    8. add 'hide_input':True to the json e.g. mine looked like { "collapsed": false, "hide_input": true, "trusted": true } after
    9. save notebook
    10. go back to the terminal and execute jupyter nbconvert --to pdf --template printviewlatex.tplx notebookname.ipynb (if your notebook is called notebookname.ipynb.ipynb)

    You should now have a document called notebookname.pdf in the directory. Hopefully it should have just the text You can see me in it...fingers crossed.

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