在 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