Toggle Source Code in Jupyter notebook

less than 1 minute read

When presenting jupyter notebooks or exporting the contents to html, pdf or whatever format, it comes in handy to hide all source code and just keep the cells output to display.

from IPython.display import HTML

HTML('''
<style>
div.output_stderr {
display: none !important;
}
</style>

<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>''')