0% found this document useful (0 votes)
12 views

Overview of Colaboratory Features - Colab

Uploaded by

Gabriel Roberto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Overview of Colaboratory Features - Colab

Uploaded by

Gabriel Roberto
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10/09/2024, 20:33 Overview of Colaboratory Features - Colab

keyboard_arrow_down Cells

A notebook is a list of cells. Cells contain either explanatory text or executable code and its output. Click a cell to select
it.

keyboard_arrow_down Code cells


Below is a code cell. Once the toolbar button indicates CONNECTED, click in the cell to select it and execute the contents
in the following ways:

Click the Play icon in the left gutter of the cell;


Type Cmd/Ctrl+Enter to run the cell in place;
Type Shift+Enter to run the cell and move focus to the next cell (adding one if none exists); or
Type Alt+Enter to run the cell and insert a new code cell immediately below it.

There are additional options for running some or all cells in the Runtime menu.

a = 10
a

10

Text cells

This is a text cell. You can double-click to edit this cell. Text cells use markdown syntax. To learn more, see our
markdown guide.

You can also add math to text cells using LaTeX to be rendered by MathJax. Just place the statement within a pair of $
−−−−−
signs. For example $\sqrt{3x-1}+(1+x)^2$ becomes √3x − 1 + (1 + x)2 .

Adding and moving cells

You can add new cells by using the + CODE and + TEXT buttons that show when you hover between cells. These buttons
are also in the toolbar above the notebook where they can be used to add a cell below the currently selected cell.

You can move a cell by selecting it and clicking Cell Up or Cell Down in the top toolbar.

Consecutive cells can be selected by "lasso selection" by dragging from outside one cell and through the group. Non-
adjacent cells can be selected concurrently by clicking one and then holding down Ctrl while clicking another. Similarly,
using Shift instead of Ctrl will select all intermediate cells.

keyboard_arrow_down Working with python


Colaboratory is built on top of Jupyter Notebook. Below are some examples of convenience functions provided.

Long running python processes can be interrupted. Run the following cell and select Runtime -> Interrupt execution
(hotkey: Cmd/Ctrl-M I) to stop execution.

https://colab.research.google.com/notebooks/basic_features_overview.ipynb#printMode=true 1/5
10/09/2024, 20:33 Overview of Colaboratory Features - Colab
import time
print("Sleeping")
time.sleep(30) # sleep for a while; interrupt me!
print("Done Sleeping")

Sleeping
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-3-626f81edbca4> in <module>()
1 import time
2 print "Sleeping"
----> 3 time.sleep(30) # sleep for a while; interrupt me!
4 print "Done Sleeping"

KeyboardInterrupt:

keyboard_arrow_down System aliases


Jupyter includes shortcuts for common operations, such as ls:

!ls /bin

arch@ dmesg* ls* pwd* true*


awk@ dnsdomainname* lsmod* readlink* umount*
basename@ domainname* mail* red@ uname*
bash* echo* mkdir* rm* uncompress*
bunzip2@ ed@ mknod* rmdir* usleep*
busybox* egrep* mktemp* run-parts* ver*
bzip2@ false* more* sed* which*
cat* fgrep* mount* sh@ wrapper_checkpoints/
chgrp* gawk@ mountpoint* sleep* zcat*
chmod* grep* mv* sort@ zcmp*
chown* gunzip* nc* stty* zdiff*
cp* gzexe* netcat@ su* zegrep*
cpio* gzip* netstat* sync* zfgrep*
csh@ hostname* nice@ tailf* zforce*
cut@ igawk@ pidof@ tar* zgrep*
date* kill* ping* tcsh@ zless*
dd* ln* ping6* tempfile* zmore*
df* login* ps* touch* znew*

That !ls probably generated a large output. You can select the cell and clear the output by either:

1. Clicking on the clear output button (x) in the toolbar above the cell; or
2. Right clicking the left gutter of the output area and selecting "Clear output" from the context menu.

Execute any other process using ! with string interpolation from python variables, and note the result can be assigned
to a variable:

# In https://github.com/ipython/ipython/pull/10545, single quote strings are ignored


message = 'Colaboratory is great!'
foo = !unset message && echo -e '{message}\n{message}\n'$message"\n$message"
foo

['Colaboratory is great!',
'Colaboratory is great!',
'Colaboratory is great!',
'Colaboratory is great!']

keyboard_arrow_down Magics

https://colab.research.google.com/notebooks/basic_features_overview.ipynb#printMode=true 2/5
10/09/2024, 20:33 Overview of Colaboratory Features - Colab

Colaboratory shares the notion of magics from Jupyter. There are shorthand annotations that change how a cell's text is
executed. To learn more, see Jupyter's magics page.

%%html
<marquee style='width: 30%; color: blue;'><b>Whee!</b></marquee>

%%html
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 450 400" width="200" height="200">
<rect x="80" y="60" width="250" height="250" rx="20" style="fill:red; stroke:black; fill-opacity:0.7" />
<rect x="180" y="110" width="250" height="250" rx="40" style="fill:blue; stroke:black; fill-opacity:0.5;" />
</svg>

keyboard_arrow_down Automatic completions and exploring code


Colab provides automatic completions to explore attributes of Python objects, as well as to quickly view documentation
strings. As an example, first run the following cell to import the numpy module.

import numpy as np

If you now insert your cursor after np and press Period( . ), you will see the list of available completions within the np
module. Completions can be opened again by using Ctrl+Space.

np

If you type an open parenthesis after any function or class in the module, you will see a pop-up of its documentation
string:

np.ndarray

The documentation can be opened again using Ctrl+Shift+Space or you can view the documentation for method by
mouse hovering over the method name.

When hovering over the method name the Open in tab link will open the documentation in a persistent pane. The View
source link will navigate to the source code for the method.

keyboard_arrow_down Exception Formatting

Exceptions are formatted nicely in Colab outputs:

https://colab.research.google.com/notebooks/basic_features_overview.ipynb#printMode=true 3/5
10/09/2024, 20:33 Overview of Colaboratory Features - Colab
x = 1
y = 4
z = y/(1-x)

---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-14-dc39888fd1d2> in <module>()
1 x = 1
2 y = 4
----> 3 z = y/(1-x)

ZeroDivisionError: integer division or modulo by zero

keyboard_arrow_down Rich, interactive outputs


Until now all of the generated outputs have been text, but they can be more interesting, like the chart below.

import numpy as np
from matplotlib import pyplot as plt

ys = 200 + np.random.randn(100)
x = [x for x in range(len(ys))]

plt.plot(x, ys, '-')


plt.fill_between(x, ys, 195, where=(ys > 195), facecolor='g', alpha=0.6)

plt.title("Fills and Alpha Example")


plt.show()

keyboard_arrow_down Integration with Drive

Colaboratory is integrated with Google Drive. It allows you to share, comment, and collaborate on the same document
with multiple people:

The SHARE button (top-right of the toolbar) allows you to share the notebook and control permissions set on it.

File->Make a Copy creates a copy of the notebook in Drive.

File->Save saves the File to Drive. File->Save and checkpoint pins the version so it doesn't get deleted from the
revision history.

File->Revision history shows the notebook's revision history.

https://colab.research.google.com/notebooks/basic_features_overview.ipynb#printMode=true 4/5
10/09/2024, 20:33 Overview of Colaboratory Features - Colab

Commenting on a cell

You can comment on a Colaboratory notebook like you would on a Google Document. Comments are attached to cells,
and are displayed next to the cell they refer to. If you have comment-only permissions, you will see a comment button on
the top right of the cell when you hover over it.

If you have edit or comment permissions you can comment on a cell in one of three ways:

1. Select a cell and click the comment button in the toolbar above the top-right corner of the cell.
2. Right click a text cell and select Add a comment from the context menu.
3. Use the shortcut Ctrl+Shift+M to add a comment to the currently selected cell.

You can resolve and reply to comments, and you can target comments to specific collaborators by typing +[email
address] (e.g., [email protected] ). Addressed collaborators will be emailed.

The Comment button in the top-right corner of the page shows all comments attached to the notebook.

https://colab.research.google.com/notebooks/basic_features_overview.ipynb#printMode=true 5/5

You might also like