1. 程式人生 > 程式設計 >jupyter notebook清除輸出方式

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 的時候,碰到了這種情況:

jupyter notebook清除輸出方式

In[ ] 後面多了個 執行符號,這使得 In[ ] 和 Out[ ] 不再對齊了,很礙眼。那麼這篇部落格,就是為你準備的,Let's go

首先,讓我們找找原因。出現這個東西是因為背後有一個CSS屬性控制了這個單元顯示,用edge(或其他瀏覽器)檢視執行圖示的元素:

jupyter notebook清除輸出方式

jupyter notebook清除輸出方式

注意 display: block 這個CSS屬性,它控制了 執行按鈕 的顯示,如果把它改為 display: none,那麼 執行按鈕 就會消失,讓我們看看效果

jupyter notebook清除輸出方式

jupyter notebook清除輸出方式

It worked! 煩人的 執行按鈕 消失了。那麼,大家已經懂了該怎麼解決了,只要更改CSS檔案裡相應的屬性值就可以了,這個CSS檔案在這裡 [ 你的anaconda安裝路徑 ]\Lib\site-packages\notebook\static\style.style.min.css

jupyter notebook清除輸出方式

開啟它,你應該有VSCode,那就用它開啟

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

jupyter notebook清除輸出方式

大功告成,重新整理你的 notebook 頁面看看效果

當然了,你也可以通過修改相應的CSS檔案,來改變notebook裡輸入程式碼的字型大小、字型樣式,輸出字型的大小等等。

以上這篇jupyter notebook清除輸出方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。