11

I want to periodically clear the output of a cell in Google Colab, which runs a local python file with !python file.py

I've tried the answers to this question on stackoverflow:

from google.colab import output output.clear()

and

from IPython.display import clear_output clear_output()

Both of those work if i run them in a cell directly and not via a local file.

dervirvel
  • 129
  • 1
  • 1
  • 3

5 Answers5

25
from IPython.display import clear_output 



clear_output()
1

You can use this in a javascript console to clear output every 1 minute:

function ClearOutput(){
    console.log("Cleared Output"); 
    document.querySelector("iron-icon[command = 'clear-focused-or-selected-outputs']").click()
}
setInterval(ClearOutput,60000)

Note that the code block for which you wish to clear the output has to be in focus. For this you can click on the following icon (shown in the image link below) when the desired cell is running:

The three dots focus on the currently running cell

1

This worked for me in Google Colab:

I found this code at the following location:

https://www.codegrepper.com/code-examples/python/jupyter+clear+cell+output+programmatically

from IPython.display import clear_output 

# your code here...

clear_output()
Manny
  • 65
  • 7
0

Instead of running

!python file.py

You can use this.

%run file.py

And it will work as expected.

korakot
  • 37,818
  • 16
  • 123
  • 144
0

IPython is for Jupyter mainly and it should work in Colab for a local file if implemented correctly. Don't forget to import the library in the main python code.