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

2019-02-05 17:07发布

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.

http://blog.nextgenetics.net/?e=102

But what if I only want to hide the first input cell of a notebook?

9条回答
三岁会撩人
2楼-- · 2019-02-05 17:44

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

查看更多
放荡不羁爱自由
3楼-- · 2019-02-05 17:45

Ok, after trying without success the answers here stated. I found this extension of kirbs.Hide_code nbextension It works just fine. But it is recommended to do the following:

First of all, make sure that you have updated your jupyter, the nbconverter, the nbconverter extensiones and the jupyter serverextension. If you did that then you can do the following in the anaconda prompt (Opened with admin priviledges):

  1. pip install hide_code
  2. jupyter nbextension install --py hide_code
  3. jupyter nbextension enable --py hide_code
  4. jupyter serverextension enable --py hide_code

Finally if you are using anaconda distribution to open your notebooks then make sure of also using these commands:

  1. jupyter nbextension install --sys-prefix --py hide_code
  2. jupyter nbextension enable --sys-prefix --py hide_code
  3. jupyter serverextension enable --sys-prefix --py hide_code

If there are no error on the execution of these commands then you will be able to see and use the hide code options in the toolbar as it is shown here:

Hide_code toolbar

Done! If you use the button for exporting and voilá!

Export Button

Good luck

查看更多
Explosion°爆炸
4楼-- · 2019-02-05 17:50

In case anyone finds excluding all code cells helpful (which is not what is asked here), you can add this flag nbconvert --TemplateExporter.exclude_code_cell=True

查看更多
该账号已被封号
5楼-- · 2019-02-05 17:58

Your solution for hiding all input cells can be altered to affect just a single cell.

Change 'div.input' to 'div.cell.code_cell.rendered.selected div.input'.

HTML('''<script>
code_show=true; 
function code_toggle() {
    if (code_show){
        $('div.cell.code_cell.rendered.selected div.input').hide();
    } else {
        $('div.cell.code_cell.rendered.selected div.input').show();
    }
    code_show = !code_show
} 

$( document ).ready(code_toggle);
</script>

To show/hide this cell's raw code input, click <a href="javascript:code_toggle()">here</a>.''')

This works because when you click the "click here" prompt on a cell's output, that cell becomes the "selected" cell and thus becomes hidden.

If your JavaScript code executes a toggle within the <script></script> tags with a line of code like this

$( document ).ready(code_toggle);

then the block will automatically ("by default") be hidden when the input cell is executed.

Keep in mind that if you do make cell inputs hidden by default, you must run the cell with the Run Cells (Ctrl+Return) option, not the Run Cells and Select/Insert Below options. These will prompt the move of the "selected" label to the next cell before executing the JavaScript, so you may end up hiding a cell that doesn't have the "click here" toggle link in its output. In which case you will have to inspect the cell and navigate through the relevant tags and change display='none'; to display='block';.

Note that this must be put at the end of any code in your cell, and that you need to have imported HTML from IPython.display before executing this code. You can do so by executing

from IPython.display import HTML
查看更多
我想做一个坏孩纸
7楼-- · 2019-02-05 18:01

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

查看更多
登录 后发表回答