How to clear the output in Google Colab via code?

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.

1

4 Answers

from IPython.display import clear_output clear_output() 
0

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

Instead of running

!python file.py 

You can use this.

%run file.py 

And it will work as expected.

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like