Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello, @Cyrille! I have a quite complicated solution that should work on Windows or any OS. I cannot take credit for it; I just adapted a solution from this site (retrieved on May 2nd, 2020), which in turn comes from this other page (retrieved on May 2nd, 2020). Since I don't know Javascript, it was a really painful process to understand this code and how to use it. Unfortunately, I wasn't able to improve the code (I just adapted it), so maybe you need a more clever person than me.

However, here is what I've got. Write the following Python script in a file, which, for the sake of the discussion, I will call "invisible_cells.py":

from IPython.core.display import display, HTML

def toggle_cell():
    tag = HTML("""
        <script>
        code_show=true; 
        function toggle_cell() {
            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(toggle_cell);
        </script>

        <a href="javascript:toggle_code_cell()">Toggle this cell</a>.""")
    display(tag)

def hide_cell():
    tag = HTML("""
        <script>
        function hide_cell() {
            $('div.cell.code_cell.rendered.selected div.input').hide();
            $('div.cell.code_cell.rendered.selected div.output').hide();
        }
        </script>

        <a href="javascript:hide_cell()">Hide permanently</a>.""")
    display(tag)

This script defined two functions: toggle_cell() will allow you to hide/unhide the contents of a cell, while hide_cell will make it permanently invisible.

You can use these functions as follows. Write the following in a cell:

import invisible_cells as ic
ic.hide_cell()

When you execute that cell, it will show you a link "Hide permanently"; when pressed, it will hide the cell and the link. From that point on, you should use the ic.hide_cell() command at the beginning of every cell you want to hide.

Since permanently hiding a cell is dangerous, I also included the ic.toggle_cell() command. If you use it at the beginning of a cell, when executed, it will show you a link "Toggle this cell", which will hide the cell, but also will show it again, just in case you need to make modifications.

This is the downside of the code: You must press the links in order to hide the cells. If you share this notebook with somebody, or even if you close and then re-open the notebook, the cells will be visible until you press the links to hide them. This is because the Javascript code uses the selected div command, which requires the cell to be activated, which in turn requires the link to be pressed. However, if you find somebody who can program Javascript, you can ask them to change the code so that the code recognizes the cell from which is executed, and automatically hides it.

I hope this helps! I am so sorry i didn't find a better solution.

Hello, @Cyrille! I have a quite complicated solution that should work on Windows or any OS. I cannot take credit for it; I just adapted a solution from this site (retrieved on May 2nd, 2020), which in turn comes from this other page (retrieved on May 2nd, 2020). Since I don't know Javascript, it was a really painful process to understand this code and how to use it. Unfortunately, I wasn't able to improve the code (I just adapted it), so maybe you need a more clever person than me.

However, here is what I've got. Write the following Python script in a file, which, for the sake of the discussion, I will call "invisible_cells.py":

from IPython.core.display import display, HTML

def toggle_cell():
    tag = HTML("""
        <script>
        code_show=true; 
        function toggle_cell() {
            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
!code_show;
        } 
        $( document ).ready(toggle_cell);
        </script>

        <a href="javascript:toggle_code_cell()">Toggle href="javascript:toggle_cell()">Toggle this cell</a>.""")
    display(tag)

def hide_cell():
    tag = HTML("""
        <script>
        function hide_cell() {
            $('div.cell.code_cell.rendered.selected div.input').hide();
            $('div.cell.code_cell.rendered.selected div.output').hide();
        }
    $(document).ready(hide_cell);
        </script>

        <a href="javascript:hide_cell()">Hide permanently</a>.""")
    display(tag)

This script defined two functions: toggle_cell() will allow you to hide/unhide the contents of a cell, while hide_cell will make it permanently invisible.

You can use these functions as follows. Write the following in a cell:

import invisible_cells as ic
ic.hide_cell()

When you execute that cell, it will show you a link "Hide permanently"; when pressed, it will hide the cell and the link. From that point on, you should use the ic.hide_cell() command at the beginning of every cell you want to hide.

Since permanently hiding a cell is dangerous, I also included the ic.toggle_cell() command. If you use it at the beginning of a cell, when executed, it will show you a link "Toggle this cell", which will hide the cell, but also will show it again, just in case you need to make modifications.

This is the downside of the code: You must press the links in order to hide the cells. If you share this notebook with somebody, or even if you close and then re-open the notebook, the cells will be visible until you press the links to hide them. This is because the Javascript code uses the selected div command, which requires the cell to be activated, which in turn requires the link to be pressed. However, if you find somebody who can program Javascript, you can ask them to change the code so that the code recognizes the cell from which is executed, and automatically hides it.

I hope this helps! I am so sorry i didn't find a better solution.

Hello, @Cyrille! I have a quite complicated solution that should work on Windows or any OS. I cannot take credit for it; I just adapted a solution from this site (retrieved on May 2nd, 2020), which in turn comes from this other page (retrieved on May 2nd, 2020). Since I don't know Javascript, it was a really painful process to understand this code and how to use it. Unfortunately, I wasn't able to improve the code (I just adapted it), so maybe you need a more clever person than me.

However, here is what I've got. Write the following Python script in a file, which, for the sake of the discussion, I will call "invisible_cells.py":

from IPython.core.display import display, HTML

def toggle_cell():
    tag = HTML("""
        <script>
        code_show=true; 
        function toggle_cell() {
            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(toggle_cell);
        </script>

        <a href="javascript:toggle_cell()">Toggle this cell</a>.""")
cell</a>""")
    display(tag)

def hide_cell():
    tag = HTML("""
        <script>
        function hide_cell() {
            $('div.cell.code_cell.rendered.selected div.input').hide();
        }
    $(document).ready(hide_cell);
        </script>

        <a href="javascript:hide_cell()">Hide permanently</a>.""")
permanently</a>""")
    display(tag)

This script defined two functions: toggle_cell() will allow you to hide/unhide the contents of a cell, while hide_cell will make it permanently invisible.

You can use these functions as follows. Write the following in a cell:

import invisible_cells as ic
ic.hide_cell()

When you execute that cell, it will show you a link "Hide permanently"; when pressed, it will hide the cell and the link. From that point on, you should use the ic.hide_cell() command at the beginning of every cell you want to hide.

Since permanently hiding a cell is dangerous, I also included the ic.toggle_cell() command. If you use it at the beginning of a cell, when executed, it will show you a link "Toggle this cell", which will hide the cell, but also will show it again, just in case you need to make modifications.

This is the downside of the code: You must press the links in order to hide the cells. If you share this notebook with somebody, or even if you close and then re-open the notebook, the cells will be visible until you press the links to hide them. This is because the Javascript code uses the selected div command, which requires the cell to be activated, which in turn requires the link to be pressed. However, if you find somebody who can program Javascript, you can ask them to change the code so that the code recognizes the cell from which is executed, and automatically hides it.

I hope this helps! I am so sorry i didn't find a better solution.