Question

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 !

Was it helpful?

Solution

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() }}
</div>
{% endblock output_group %}

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

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


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

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></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 {
    width:100%;
    height:3px;  
    background:#eeeeee;
    position:absolute;
    bottom:0px;
    left:0;
    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;
}
</style>

<script>
$(document).ready(function(){
  $(".output_hidden").click(function(){
      $(this).prev('.input_hidden').slideToggle();
  });
  $(".input_hidden").click(function(){
      $(this).next('.output_hidden').slideToggle();
  });
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");
        $prompt.click(function () {
            var curHeight = $in_hidden.height();
            if (curHeight == slideHeight) {
                $in_hidden.animate({
                    height: defHeight
                }, "normal");
                $gradient.fadeOut();
            } 
            else {
                $in_hidden.animate({
                    height: slideHeight
                }, "normal");
                $gradient.fadeIn();
            }
            return false;
        });
    }
});
});

</script>
{%- endblock header -%}

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

Have fun !

OTHER TIPS

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

HTML('''<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>''')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top