Friday, 30 May 2014 at 2:22 pm

I recently started using Ipython notebook and it has quickly become essential to my workflow. However, when discussing the data informally with colleagues, I wanted the code cells to be hidden as to not distract from the figures.

The newest IPython notebook version do not allow executing javascript in markdown cells anymore, so adding a new markdown cell with the following javascript code will not work anymore to hide your code cells:

$('div.input').hide()

I also didn't want to resort to adding an ugly extra markdown cell, so I went into the IPython jinja template and added a new menu item under "view" for toggling the code cells. 

The IPython notebook template on my OSX laptop was located at:

/Library/Python/2.7/site-packages/IPython/html/templates/notebook.html

I added a new javascript toggle function in the script tag. Where it used to be:

<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
window.mathjax_url = "{{mathjax_url}}";
</script> 

I changed it to: (added code in red)

<script type="text/javascript">
// MathJax disabled, set as null to distingish from *missing* MathJax,
// where it will be undefined, and should prompt a dialog later.
window.mathjax_url = "{{mathjax_url}}";

code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
</script>

Then under the view menu:

<ul id="view_menu" class="dropdown-menu">
<li id="toggle_header"....
....
..
</ul>

I added a new menu item:

<li id="toggle_toolbar"
title="Show/Hide code cells">
<a href="javascript:code_toggle()">Toggle Code Cells</a></li>

Save the notebook.html file and restart your ipython notebook. You should now have a new third menu item under "view" that'll allow you to toggle the code cells.

This is a pretty dirty hack though and it might conflict with future updates. So please make a backup version of the notebook.html before you modify it.








Search

Categories


Archive