如何将ipython笔记本转换为带有折叠输出(和/或输入)的html

6 投票
3 回答
5571 浏览
提问于 2025-04-18 08:43

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

所以我用这个方法把它转换成了 html 格式:

ipython nbconvert my_notebook.ipynb

但是我遇到的问题是,输出内容太长了,阅读起来很困难。我想知道在 html 版本中,是否可以像在笔记本查看器中那样,有折叠或滚动的选项。

简单来说,我想要这样的效果: 输出示例 在这里输入图片描述

但是在 html 版本中。这可能实现吗?

谢谢你的帮助!

3 个回答

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>''')
0

Ipython 2.0现在可以直接在笔记本中保存为HTML格式了。

在旧版本中,如果代码行数超过100行,就会自动生成滚动条。如果这些滚动条仍然出现,你的HTML输出也应该有这些滚动条,对吧?

4

我找到了我想要的东西,感谢这篇博客,它正好满足了我的需求。

我稍微修改了一下,让它可以在ipython 2.1上运行[编辑:也适用于Jupyter],并结合了输入和输出隐藏的技巧。

它的功能:

当你打开这个html文件时,所有的输入内容会显示出来,而输出内容会被隐藏。点击输入框后,输出框会显示出来。一旦你同时看到这两个框,你可以通过点击其中一个来隐藏另一个。

编辑:现在它可以隐藏较长的输入内容,默认情况下会显示大约一行。你可以通过点击输入框的编号来显示所有内容。这在你没有输出时很方便(比如你想在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来定制你的笔记本!

玩得开心!

撰写回答