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:
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.
- 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) - 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.
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
How to Display JavaScript Datetime in 12 Hour Am/Pm Format
Converting Async/Await Function to Normal Es5 Targetting Ie 11
What Is the Opposite of Evt.Preventdefault();
Modifying a Query String Without Reloading the Page
How to Return Blank String If Object Is Undefined in Angularjs Forms
Reactjs How to Call a Component Function from Another Function on the Same File
Datatables/Tabletools: Format Data as Text When Exporting to Excel
How to Load a Font File in Vuejs and Webpack
Json.Stringify Without Quotes on Properties
Warning: Failed Proptype: Invalid Prop 'Component' Supplied to 'Route'
How to Use Fetch Within a For-Loop, Wait for Results and Then Console.Log It
How to Let a User Download Multiple Files When a Button Is Clicked
Show 2 Items Per Row[React Native]
Javascript Onclick Increment Number