如何隐藏ipython笔记本中的单元格与nbviewer可视化代码?

我有使用NBviewer可视化的ipython / jupyter笔记本。

如何隐藏NBviewer呈现的笔记本中的所有代码,以便仅显示代码(如图表和表格)和降价单元的输出?

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

最新的IPython笔记本版本不允许在降价单元格中执行javascript,因此,使用以下JavaScript代码添加新的降价单元格将无法再隐藏您的代码单元格(请参阅此链接 )

改变〜/ .ipython / profile_default /静态/自定义/ custom.js如下:

 code_show=true; function code_toggle() { if (code_show){ $('div.input').hide(); } else { $('div.input').show(); } code_show = !code_show } $([IPython.events]).on("app_initialized.NotebookApp", function () { $("#view_menu").append("<li id=\"toggle_toolbar\" title=\"Show/Hide code cells\"><a href=\"javascript:code_toggle()\">Toggle Code Cells</a></li>") }); 

我会使用hide_input_allhide_input_allhttps://github.com/ipython-contrib/IPython-notebook-extensions )。 就是这样:

  1. 找出您的IPython目录是:

     from IPython.utils.path import get_ipython_dir print get_ipython_dir() 
  2. 下载nbextensions并将其移动到IPython目录。

  3. 在IPython目录(我的profile_default / static / custom )中编辑你的custom.js文件,类似于nbextensions目录下的custom.example.js

  4. 将此行添加到custom.js中

     IPython.load_extensions('usability/hide_input_all') 

IPython Notebook现在有一个button来切换代码单元格,而不pipe工作簿。

我写了一些代码来实现这一点,并添加了一个button来切换代码的​​可见性。

下面是笔记本顶部的代码单元格:

 from IPython.display import display from IPython.display import HTML import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True) # This line will hide code by default when the notebook is exported as HTML di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True) # This line will add a button to toggle visibility of code blocks, for use with the HTML export version di.display_html('''<button onclick="jQuery('.input_area').toggle(); jQuery('.prompt').toggle();">Toggle code</button>''', raw=True) 

你可以在这里看到一个NBviewer的例子 。

更新:这将有一些Jupyter中的Markdown单元有趣的行为,但它在笔记本的HTML导出版本中工作正常。

为了更好地显示打印的文档或报告,我们还需要删除button以及显示或隐藏某些代码块的function。 这就是我使用的(只需复制粘贴到你的第一个单元格):

 # This is a cell to hide code snippets from displaying # This must be at first cell! from IPython.display import HTML hide_me = '' HTML('''<script> code_show=true; function code_toggle() { if (code_show) { $('div.input').each(function(id) { el = $(this).find('.cm-variable:first'); if (id == 0 || el.text() == 'hide_me') { $(this).hide(); } }); $('div.output_prompt').css('opacity', 0); } else { $('div.input').each(function(id) { $(this).show(); }); $('div.output_prompt').css('opacity', 1); } code_show = !code_show } $( document ).ready(code_toggle); </script> <form action="javascript:code_toggle()"><input style="opacity:0" type="submit" value="Click here to toggle on/off the raw code."></form>''') 

然后在你的下一个单元格中

 hide_me print "this code will be hidden" 

 print "this code will be shown" 

使用扩展基本iPython笔记本的Runtools:

https://github.com/ipython-contrib/IPython-notebook-extensions/wiki/Runtools

这将呈现IPython笔记本输出。 但是,您会注意到能够查看input代码。 您可以复制笔记本,然后添加此代码,如果需要与不需要查看代码的人共享。

 from IPython.display import HTML HTML('''<script> $('div .input').hide()''') 

这是p3trusbuild议的另一个解决scheme:

 $([IPython.events]).on('notebook_loaded.Notebook', function(){ IPython.toolbar.add_buttons_group([ { 'label' : 'toggle input cells', 'icon' : 'icon-refresh', 'callback': function(){$('.input').slideToggle()} } ]); }); 

正如p3trus所描述的那样 :“它在ipython笔记本工具栏中添加一个button来隐藏/显示input代码单元,要使用它,必须将custom.js文件放入.ipython_<profile name>/static/custom/文件夹,在哪里 是正在使用的ipythonconfiguration文件。“

我自己的意见:我validation了这个解决scheme,它适用于iPython 3.1.0。

(纸)打印或保存为HTML

对于那些希望打印输出结果的人来说,上面的答案似乎并不能给出一个好的结果。 但是,使用@Max Masnick的代码并添加以下代码,可以将其打印在完整的A4页面上。

 from IPython.display import display from IPython.display import HTML import IPython.core.display as di di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True) CSS = """#notebook div.output_subarea {max-width:100%;}""" #changes output_subarea width to 100% (from 100% - 14ex) HTML('<style>{}</style>'.format(CSS)) 

缩进的原因是由Max Masnick删除的提示部分意味着所有内容都会在输出上左移。 然而,这对于限制在最大宽度的输出的最大宽度没有任何影响max-width:100%-14ex; 。 这将output_subarea的最大宽度更改为max-width:100%;

被接受的解决scheme也适用于Julia Jupyter / IJulia,但有以下修改:

 display("text/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>""") 

特别注意:

  • 使用displayfunction
  • 逃避$符号(否则看作是一个variables)

尽pipe隐藏了所有的解决scheme,但是你仍然可以得到[<matplotlib.lines.Line2D at 0x128514278>]以上的数据,你可能不想要。

如果你真的想摆脱input,而不是隐藏它,我认为最简洁的解决scheme是将你的数字保存到磁盘上的隐藏单元格中,然后使用例如![Caption](figure1.png)将图像包含在Markdown单元格中![Caption](figure1.png)

这里提供了一个很好的解决scheme,适用于输出到HTML的笔记本。 该网站甚至连接到这个SOpost,但我没有看到克里斯的解决scheme在这里! (克里斯,你在哪里?)

这与harshil所接受的答案基本上是相同的解决scheme,但它具有在导出的HTML中隐藏切换代码本身的优点。 我也喜欢这种方法避免了IPython HTMLfunction的需要。

要实现此解决scheme,请将以下代码添加到笔记本顶部的“Raw NBConvert”单元格中:

 <script> function code_toggle() { if (code_shown){ $('div.input').hide('500'); $('#toggleButton').val('Show Code') } else { $('div.input').show('500'); $('#toggleButton').val('Hide Code') } code_shown = !code_shown } $( document ).ready(function(){ code_shown=false; $('div.input').hide() }); </script> <form action="javascript:code_toggle()"><input type="submit" id="toggleButton" value="Show Code"></form> 

然后只需将笔记本导出为HTML。 笔记本顶部会有一个切换button来显示或隐藏代码。

克里斯也提供了一个例子。

我可以validation这在Jupyter 5.0.0中的工作