在 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