How to convert an ipython notebook to html with collapsed output (and/or input)

I have an ipython notebook I'd like to share with colleagues who may not have ipython installed.

So I converted it to html with :

ipython nbconvert my_notebook.ipynb

But my problem is that I have very long outputs which make the reading difficult, and I'd like to know whether it's possible to have the collapse or scroll option of the notebook viewer on the html version.

Basically, I'd like this : output example enter image description here

But in the html version. Is this even possible ?

Thanks for helping !



I found what I wanted thanks to that blog which does exactly what I wanted.

I modified it a bit to make it work with ipython 2.1 [edit: works aslo with Jupyter], and mixed the input and output hidding tricks.

What it does:

When opening the html file, all input will be shown and output hidden. By clicking on the input field it will show the output field. And once you have both fields, you can hide one by clicking the other.

edit: It now hides long input, and about 1 line is always shown (by defa. You can show everything by clicking on the input number. This is convenient when you don't have output (like a definition of a long function you'd like to hide in your HTML doc)

You need to add a template while doing nbconvert :

ipython nbconvert --template toggle my_notebook.ipynb

where toggle is a file containing :

{%- extends 'full.tpl' -%}

{% block output_group %}
<div class="output_hidden">
{{ super() }}
{% endblock output_group %}

{% block input_group -%}
<div class="input_hidden">
{{ super() }}
{% endblock input_group %}

{%- block input -%}
<div class="in_container">
<div class="in_hidden">
{{ super() }}
<div class="gradient">
{%- endblock input -%}

{%- block header -%}
{{ super() }}

<script src=""></script>

<style type="text/css">
div.output_wrapper {
  margin-top: 0px;
.output_hidden {
  display: block;
  margin-top: 5px;
.in_hidden {
   width: 100%;
   overflow: hidden;
   position: relative;

.in_container {
    width: 100%;
    margin-left: 20px;
    margin-right: 20px;

.gradient {
    display: none;
    opacity: 0.4;
    border-bottom: 2px dashed #000;
div.input_prompt {
    color: #178CE3;
    font-weight: bold;
div.output_prompt {
    color: rgba(249, 33, 33, 1);
    font-weight: bold;

var slideHeight = 25;
$(".in_container").each(function () {
    var $this = $(this);
    var $in_hidden = $this.children(".in_hidden");
    var defHeight = $in_hidden.height();
    if (defHeight >= 61) {
        var $prompt = $this.prev(".input_prompt");
        var $gradient = $in_hidden.children(".gradient");
        $in_hidden.css("height", slideHeight + "px");
        $gradient.css("display", "block");
        $ () {
            var curHeight = $in_hidden.height();
            if (curHeight == slideHeight) {
                    height: defHeight
                }, "normal");
            else {
                    height: slideHeight
                }, "normal");
            return false;

{%- endblock header -%}

Basically, you can inject whatever javascript and css you want to customize your notebook at will!

Have fun !


Ipython 2.0 now includes save to HTML directly within the notebook.

The scrolling bars were automatically created with lines > 100 in an older version. Docs If they still show, your html output should also have the bars, no?


You can use below code to convert your notebook into HTML with code as collapsed.

from IPython.display import HTML

function code_toggle() {
 if (code_show){
 } else {
 code_show = !code_show
$( document ).ready(code_toggle);
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.