Hiding Input Cells On Jupyter Notebook

December 26, 2018    machine-learning deep-learning datascience

Why Needed ?

Sometimes we don’t want to show the code during presentation of the notebook, or sometimes the code can look really clumsy. Taking care of the code while tearing your hairs to find a better solution to a problem simultaneously is quite a mammoth task. Most tutorials on the web would suggest you to install nbextensions, an open-source notebook extension package for Jupyter notebooks. Definitely that is clean and easy, but I just don’t like installing things and junking up my workstation, rather I’d write a custom code to solve the problem. Here, I have a solution which probably many of you may find useful to hide the input cells of a notebook without installing anything extra at all.

How To Use ?

On the first cell of your notebook just paste this code snippet and Run the cell (Ctrl-Enter or Shift-Enter).

# This is a cell to hide code snippets from displaying
# This must be at first cell!

from IPython.display import HTML

hide_me = ''
function code_toggle() {
  if (code_show) {
    $('div.input').each(function(id) {
      el = $(this).find('.cm-variable:first');
      if (id == 0 || el.text() == 'hide_me') {
    $('div.output_prompt').css('opacity', 0);
  } else {
    $('div.input').each(function(id) {
    $('div.output_prompt').css('opacity', 1);
  code_show = !code_show
$( document ).ready(code_toggle);
<form action="javascript:code_toggle()"><input style="opacity:0" type="submit" value="Click here to toggle on/off the raw code."></form>''')

Once you run the cell, you will probably see it disappear. So here went the code, but now how to use it!

To hide a cell, just write hide_me as the first line of the cell followed by whatever you need to write after it. That’s it.

I have this tested for Jupyter notebook server version 5.3.1 on Python 3.6.1. This should work for you all well!