In Google colab I execute command line scripts by place a !
in front of the line and executing the cell.
For example
!pip install adjustText
If I want to prevent output of this cell, I can do this
%%capture
!pip install adjustText
However, I have a situation where I execute the command line scripts via a function, and suppress output for that command line only, without suppressing the output of the cell from which it's being executed
For example
Cell1:
%%capture
def installAdjust():
!pip install adjustText
Cell2:
for v in range(10):
print(v)
installAdjust()
This does not suppress the output from !pip install adjustText
. I do not want to suppress the non-command line output from Cell2, so I can Not do this
Cell2:
%%capture
for v in range(10):
print(v)
installAdjust()
Also, this doesn't work either
Cell1:
def installAdjust():
%%capture
!pip install adjustText
Just select the cell and press Ctrl+ M O . This will hide the output of the cell. This option is also available under the view tab.
Run the following cell and select Runtime -> Interrupt execution (hotkey: Cmd/Ctrl-M I) to stop execution.
It's already possible to run terminal commands in a Colab notebook — all you need to do is prepend an exclamation to the command, or use the %%shell command, but sometimes you might prefer the convenience an interactive shell.
Working with Google Sheets Colab also supports rich outputs such as charts. Type in the following code in the Code cell. Note that the graphical output is shown in the output section of the Code cell. Likewise, you will be able to create and display several types of charts throughout your program code.
you can use '%%capture' magic function in a cell(without quotes) to suppress the output of that particular cell whether it uses a command-line code or some python code, magic function is basically a property of jupyter notebooks but since google colab is built over this, it will work there also. eg:
%%capture
!wget https://github.com/09.10-20_47_44.png
Use capture_output
from python's utilities:
from IPython.utils import io
for v in range(10):
print(v)
with io.capture_output() as captured:
installAdjust()
For the future, whenever a magic function doesn't suffice, search for the core properties being accessed and access them yourself.
Answer sourced from: How do you suppress output in IPython Notebook?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With