简体   繁体   English

如何隐藏代码并重新运行 jupyter 笔记本中的所有单元格?

[英]How can I hide code and rerun all cells in a jupyter notebook?

I'd like to add some sort of functionality at the beginning of a Jupyter Notebook that hides / shows all cells and reruns all cells.我想在 Jupyter Notebook 的开头添加某种功能,以隐藏/显示所有单元格并重新运行所有单元格。 What I'd like to end up with is a set of charts that are refreshed when all cells are re-run.我想要结束的是一组图表,当所有单元格重新运行时会刷新这些图表。


The details and what I've tried:详细信息和我尝试过的内容:

The post IPython - Run all cells below from a widget shows how you can add a button to rerun all cells below.文章IPython - 从小部件运行下面的所有单元格显示了如何添加按钮以重新运行下面的所有单元格。 And the post How to hide code from cells in ipython notebook visualized with nbviewer?以及如何从使用 nbviewer 可视化的 ipython notebook 中的单元格隐藏代码? . . With this setup in two different cells I end up with this:通过在两个不同的单元格中进行此设置,我最终得到了以下结果:

在此处输入图片说明

When the cells are collapsed it looks like this:当单元格折叠时,它看起来像这样:

在此处输入图片说明

And this works pretty well, but I'm just really curious if it's possible to format the buttons so that they look the same.这很好用,但我真的很好奇是否可以设置按钮的格式以使它们看起来相同。 And maybe it's possible to align them as output from the same cell?也许可以将它们对齐为同一单元格的输出? I've tried to do just that by having the two snippets in the same cell, but now it seems that the Hide button is overwritten by the Refresh button :我试图通过将两个片段放在同一个单元格中来做到这一点,但现在似乎Hide buttonRefresh button覆盖:

Snippet 1:片段 1:

from IPython.display import HTML

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

from IPython.display import Javascript, display
from ipywidgets import widgets

def run_all(ev):
    display(Javascript('IPython.notebook.execute_cells_below()'))

button = widgets.Button(description="Refresh")
button.on_click(run_all)
display(button)

And now I end up with this:现在我得到了这个:

Output 1:输出 1:

在此处输入图片说明

Does anyone know how to make this a bit more elegant?有谁知道如何使它更优雅一点?

I really hope someone is able to provide a better answer, but having tried and failed for a couple of hours, I've found this:我真的希望有人能够提供更好的答案,但是尝试并失败了几个小时后,我发现了这一点:

By simply mixing a few parts of the two snippets in the question, I'm able to set up a Refresh button in the same format as the Hide Code buttion :通过简单地混合问题中两个片段的几个部分,我可以设置一个与Hide Code buttion格式相同的Refresh button Hide Code buttion

Ouptput / Notebook View:输出/笔记本视图:

在此处输入图片说明

But this still requiers two code snippets in two different cells, as well as some test code in a third cell:但这仍然需要两个不同单元格中的两个代码片段,以及第三个单元格中的一些测试代码:

Cell / snippet 1:单元格/片段1:

from IPython.display import HTML

HTML('''<script>
  function code_toggle() {
    if (code_shown){
      $('div.input').hide('500');
      $('#toggleButton').val('Display 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="Display code"></form>''')

Cell / snippet 2:单元格/片段 2:

HTML('''<script>

</script>
<form action="javascript:IPython.notebook.execute_cells_below()"><input type="submit" id="toggleButton" value="Refresh"></form>''')

Cell / snippet 3:单元格/片段 3:

try: x
except NameError: x = None

if x is None:
    x = 0
    print(x)
else:
    x = x + 1
    print(x)

However, I'm still not able to display the two buttons beautifully side by side, and the display flickers when I hit Refresh .但是,我仍然无法漂亮地并排显示这两个按钮,并且当我点击Refresh时显示会闪烁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM