6

我有一个ipython notebook我想与可能没有安装 ipython 的同事分享。

所以我将它转换为html:

ipython nbconvert my_notebook.ipynb

但是我的问题是我的输出很长,这使阅读变得困难,我想知道是否可以在 html 版本上使用笔记本查看器的折叠或滚动选项。

基本上,我想要这个:输出示例 在此处输入图像描述

但是在html版本中。这甚至可能吗?

感谢您的帮助!

4

3 回答 3

4

多亏了那个完全符合我要求的博客,我找到了我想要的东西。

我对其进行了一些修改以使其与 ipython 2.1 一起使用 [编辑:与 Jupyter 一起使用],并混合了输入和输出隐藏技巧。

它能做什么:

打开 html 文件时,将显示所有输入并隐藏输出。通过单击输入字段,它将显示输出字段。一旦你拥有这两个字段,你可以通过单击另一个来隐藏一个。

编辑:它现在隐藏了长输入,并且总是显示大约 1 行(通过默认值。您可以通过单击输入编号来显示所有内容。当您没有输出时这很方便(就像您定义长函数一样)我想隐藏在你的 HTML 文档中)

您需要在执行 nbconvert 时添加模板:

ipython nbconvert --template toggle my_notebook.ipynb

其中 toggle 是一个文件,其中包含:

{%- 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 -%}

基本上,您可以随意注入您想要自定义笔记本的任何 javascript 和 css!

玩得开心 !

于 2014-06-05T18:28:01.943 回答
0

Ipython 2.0 现在包括直接在笔记本中保存到 HTML。

在旧版本中,滚动条是自动创建的,行数 > 100。Docs如果他们仍然显示,你的 html 输出也应该有吧,不是吗?

于 2014-06-05T17:20:44.387 回答
0

您可以使用下面的代码将您的笔记本转换为 HTML,其中代码为折叠状态。

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>''')
于 2019-08-20T12:59:05.727 回答