How to Hide Code from Cells in Ipython Notebook Visualized With Nbviewer

How to hide code from cells in ipython notebook visualized with nbviewer?

from IPython.display import HTML

HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')

How can I hide code and rerun all cells in a jupyter notebook?

I really hope someone is able to provide a better answer, but having tried and failed for a couple of hours, I've found this:

By simply mixing a few parts of the two snippets in the question, I'm able to set up a Refresh button in the same format as the Hide Code buttion:

Ouptput / Notebook View:

Sample Image

But this still requiers two code snippets in two different cells, as well as some test code in a third cell:

Cell / snippet 1:

from IPython.display import HTML

HTML('''<script>
function code_toggle() {
if (code_shown){
$('div.input').hide('500');
$('#toggleButton').val('Display code')
} else {
$('div.input').show('500');
$('#toggleButton').val('Hide Code')
}
code_shown = !code_shown
}

$( document ).ready(function(){
code_shown=false;
$('div.input').hide()
});
</script>
<form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Display code"></form>''')

Cell / snippet 2:

HTML('''<script>

</script>
<form action="javascript:IPython.notebook.execute_cells_below()"><input type="submit" id="toggleButton" value="Refresh"></form>''')

Cell / snippet 3:

try: x
except NameError: x = None

if x is None:
x = 0
print(x)
else:
x = x + 1
print(x)

However, I'm still not able to display the two buttons beautifully side by side, and the display flickers when I hit Refresh.

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

This is now built into nbconvert (as of 5.3.0) using tags.

Here's an example removing a specific cell from the output. Using this notebook. The example has three cells: a markdown cell, a code cell that will be hidden, and a code cell that will not be hidden.

  1. Add the remove_cell tag to any cells you want to hide using the tag editor built into the notebook or JupyterLab (the specific name "remove_cell" doesn't matter)
  2. Convert with nbconvert
jupyter nbconvert nbconvert-example.ipynb --TagRemovePreprocessor.remove_cell_tags='{"remove_cell"}'

Any cells with the tag remove_cell will be removed from the output.

hidden

In addition to entire cells, you can filter just inputs or just outputs:

  • TagRemovePreprocessor.remove_input_tags
  • TagRemovePreprocessor.remove_single_output_tags
  • TagRemovePreprocessor.remove_all_outputs_tags


Related Topics



Leave a reply



Submit