Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress output in Google Colaboratory cell which executes a command line script (line starts with `!`) via a function

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
like image 342
Peter Force Avatar asked Jun 23 '19 20:06

Peter Force


People also ask

How do I turn off output in Google Colab?

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.

How do you stop a cell from running in Colab?

Run the following cell and select Runtime -> Interrupt execution (hotkey: Cmd/Ctrl-M I) to stop execution.

How do you execute a command in Colab?

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.

How do I display output in Google Colab?

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.


2 Answers

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
like image 120
bhanu pratap Avatar answered Sep 20 '22 13:09

bhanu pratap


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?

like image 38
Yaakov Bressler Avatar answered Sep 19 '22 13:09

Yaakov Bressler