Hide Code When Sharing IPython Notebooks

When sharing IPython notebooks with non-programmers, I don't want to cause confusion by including a bunch of code.

To this end, I wrote a snippet of Python and JavaScript that can be put in a code cell at the top of a notebook. When a notebook with this code is exported as HTML or viewed with nbviewer.ipython.org, code blocks will be hidden by default. A "Toggle code" button is also added in case you want to see the hidden code.

Here is the code snippet to put in the cell at the top of the notebook:

And here is an example of how this works in nbviewer.ipython.org.

(The source code for this example and the accompanying HTML output are available here.)


🌟 Was this page helpful? Please let me know with this quick, 3 question survey.

22 responses
Hi. Thank you for your post. Is it possible to remove the button ? So that we "publish" only the outputs ? Thanks.
@Shirin -- yes, just comment out line 7.
I just tried and realized that the markdown cells are shown as if they were not executed and below their executed output. Is there a way to only show the executed output? Thanks!
Amazingly helpful. Thank you
Thank you!
Really good
Really really good. Thank you very much.
15 visitors upvoted this post.