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