jupyter notebook清除输出方式

在 jupyter notebook参数化运行python 时,怕输出太多文件太大,想及时清除 notebook 的输出。

在别人代码里看到用 easydl 的 clear_output()。调用很简单:

from easydl import clear_output

print('before')
clear_output() # 清除输出
print('after')

查它源码:clear_output

def clear_output():
  """
  clear output for both jupyter notebook and the console
  """
  import os
  os.system('cls' if os.name == 'nt' else 'clear')
  if is_in_notebook():
    from IPython.display import clear_output as clear
    clear()

terminal/console 的输出调系统的 clear/cls 命令清除

notebook 的输出用 IPython.display.clear_output() 清除

其中 is_in_notebook() 也是 easydl 的函数,用来判断是不是在 notebook 里。

查它源码:is_in_notebook

def is_in_notebook():
  import sys
  return 'ipykernel' in sys.modules

补充知识:Jupyter notebook 如何去掉 input输入框 前面的 运行按钮?

如果你最近在使用Jupyter notebook 的时候,碰到了这种情况:

打开它,你应该有VSCode,那就用它打开

定位到 10661 行,修改它为 display: none

大功告成,刷新你的 notebook 页面看看效果

当然了,你也可以通过修改相应的CSS文件,来改变notebook里输入代码的字体大小、字体样式,输出字体的大小等等。

以上这篇jupyter notebook清除输出方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持来客网。