如何隐藏代码并重新运行Jupyter笔记本中的所有单元格? [英] How can I hide code and rerun all cells in a jupyter notebook?

查看:206
本文介绍了如何隐藏代码并重新运行Jupyter笔记本中的所有单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在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. What I'd like to end up with is a set of charts that are refreshed when all cells are re-run.

详细信息和我尝试过的操作:

帖子 IPython-从小部件运行下面的所有单元格显示如何添加按钮以重新运行下面的所有单元格.和帖子如何从中隐藏代码使用nbviewer可视化ipython笔记本中的单元格?.在两个不同的单元格中进行此设置后,我得到以下结果:

The post IPython - Run all cells below from a widget shows how you can add a button to rerun all cells below. And the post How to hide code from cells in ipython notebook visualized with nbviewer?. With this setup in two different cells I end up with this:

当单元折叠时,它看起来像这样:

When the cells are collapsed it looks like this:

这很好用,但是我真的很好奇是否可以设置按钮的格式,使它们看起来一样.也许可以将它们作为同一单元的输出进行对齐?我试图通过在同一个单元格中包含两个代码片段来做到这一点,但是现在看来Hide buttonRefresh button覆盖了:

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:

代码段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)

现在我结束了:

输出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:

只需将问题中两个片段的一部分混合在一起,就可以以与Hide Code buttion相同的格式设置Refresh button:

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:

输出/笔记本视图:

但这仍然需要两个不同单元格中的两个代码段,以及第三个单元格中的一些测试代码:

But this still requiers two code snippets in two different cells, as well as some test code in a third cell:

单元格/摘录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>''')

单元格/摘录2:

HTML('''<script>

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

单元格/代码段3:

try: x
except NameError: x = None

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

但是,我仍然不能很好地并排显示两个按钮,并且当我按下Refresh时,屏幕会闪烁.

However, I'm still not able to display the two buttons beautifully side by side, and the display flickers when I hit Refresh.

这篇关于如何隐藏代码并重新运行Jupyter笔记本中的所有单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆