jupyter notebook清除輸出

jupyter notebook參數化運行python 時,怕輸出太多文件太大,想及時清除 notebook 的輸出。
在別人代碼裏看到用 easydlclear_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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章