- Use Google Colab to Run Python in the Cloud
- What is Google Colab?
- How to get started
- How to use Colab
- Import packages
- Adding collaborators
- Connect to google drive
- Python Installation — Colab Notebook
- Open a Colab notebook
- New notebook
- Google Drive
- Colab interface
- Existing notebook
- Google Drive
- Colab interface
- Import API and get credentials
- Import the API
- Authenticate and initialize
- Test the API
- Map visualization
- Static image
- Interactive map
- Chart visualization
Use Google Colab to Run Python in the Cloud
Join the community!
Visit our GitHub or LinkedIn page to join the Tilburg Science Hub community, or check out our contributors’ Hall of Fame!
Want to change something or add new content? Click the Contribute button!
What is Google Colab?
Colab is a product from Google Research that allows anybody with an internet connection to write and execute python code through a browser. Colab is free to use, and there is no setup on your computer needed. It is especially useful if you have a slow computer since Google hosts your Jupyter notebook and thus uses their GPU free of charge. Or if you want to work together on the same code, since you can collaborate on the same cloud file with multiple editors.
How to get started
It is time to create your own Colab notebook!
- Go to https://colab.research.google.com/
- Press New notebook, or if you already have a file, choose this one from the list
- Save your notebook to either Google drive or GitHub via File >Save a copy in …
How to use Colab
Once you are in your Colab notebook you can use it just like any offline Jupyter notebook.
Type your code in the darker gray box en pres the run arrow to run the code. Your output will be placed below the box. To create a new code box, press + Code in the navigation bar.
Import packages
Colab environment already has a number of pre-installed packages such as pandas , pytorch , scipy , tenserflow and numpy . To check all installed packages use pip freeze
If a package is already installed, import these by importing them with import
If a package is not yet installed, install the package inside the Colab notebook by adding the code !pip install
!pip install cartopy import cartopy
Adding collaborators
To add other collaborators, go to the right corner and press Share. Now choose to either invite people via e-mail or by sending them a link. It is also possible to change the permissions to ‘Commenter’ or ‘Viewer’ by pressing .
Connect to google drive
If you want to import a dataset or any other file stored on your pc, the easiest way to do so is by connecting Google Colab to your Google drive. To do so, follow the steps below!
from google.colab import drive drive.mount('/content/drive')
- Press Connect to Google Drive
- Login to your Google account
- Press Allow
You can now import, and export files like you are used to with offline notebooks
Python Installation — Colab Notebook
The Earth Engine Python API can be deployed in a Google Colaboratory notebook. Colab notebooks are Jupyter notebooks that run in the cloud and are highly integrated with Google Drive, making them easy to set up, access, and share. If you are unfamiliar with Google Colab or Jupyter notebooks, please spend some time exploring the Colab welcome site.
The following sections describe deploying Earth Engine in Google Colab and visualizing maps and charts using third‑party Python packages.
Note: Installing the Earth Engine API and authenticating are necessary steps each time you begin working with a Colab notebook. This guide demonstrates setup and testing with a new Colab notebook, but the process applies to shared and saved notebooks as well. If you don’t have Earth Engine access, please register.
Open a Colab notebook
Notebooks can be opened from either Google Drive or the Colaboratory interface.
New notebook
Google Drive
Open Google Drive and create a new file.
- New > More > Colaboratory
- Right click in a folder and select More > Colaboratory from the context menu.
Colab interface
Visit the Colab site and create a new file.
- File > New > New Python 3 notebook
- If you have interacted with Colab previously, visiting the above linked site will provide you with a file explorer where you can start a new file using the dropdown menu at the bottom of the window.
Existing notebook
Existing notebook files (.ipynb) can be opened from Google Drive and the Colab interface.
Google Drive
Colab notebooks can exist in various folders in Google Drive depending on where notebooks files were created. Notebooks created in Google Drive will exist in the folder they were created or moved to. Notebooks created from the Colab interface will default to a folder called ‘Colab Notebooks’ which is automatically added to the ‘My Drive’ folder of your Google Drive when you start working with Colab.
Colab files can be identified by a yellow ‘CO’ symbol and ‘.ipynb’ file extension. Open files by either doubling clicking on them and selecting Open with > Colaboratory from the button found at the top of the resulting page or by right clicking on a file and selecting Open with > Colaboratory from the file’s context menu.
Colab interface
Opening notebooks from the Colab interface allows you to access existing files from Google Drive, GitHub, and local hardware. Visiting the Colab interface after initial use will result in a file explorer modal appearing. From the tabs at the top of the file explorer, select a source and navigate to the .ipynb file you wish to open. The file explorer can also be accessed from the Colab interface by selecting File > Open notebook or using the Ctrl+O keyboard combination.
Import API and get credentials
This section demonstrates how to import the Earth Engine Python API and authenticate access. This content is also available as a Colab notebook:
The Earth Engine API is included by default in Google Colaboratory so requires only importing and authenticating. These steps must be completed for each new Colab session or if you restart your Colab kernel or if your Colab virtual machine is recycled due to inactivity.
Import the API
Run the following cell to import the API into your session.
Authenticate and initialize
Run the ee.Authenticate function to authenticate your access to Earth Engine servers and ee.Initialize to initialize it. Add a code cell, enter the following lines, and run the cell.
# Trigger the authentication flow. ee.Authenticate() # Initialize the library. ee.Initialize()
You’ll be asked to authorize access to your Earth Engine account. Follow the instructions printed to the cell to complete this step.
Test the API
Test the API by printing the elevation of Mount Everest. Note that before using the API you must initialize it. Run the following Python script in a new cell.
# Print the elevation of Mount Everest. dem = ee.Image('USGS/SRTMGL1_003') xy = ee.Geometry.Point([86.9250, 27.9881]) elev = dem.sample(xy, 30).first().get('elevation').getInfo() print('Mount Everest elevation (m):', elev)
Map visualization
ee.Image objects can be displayed to notebook output cells. The following two examples demonstrate displaying a static image and an interactive map.
Static image
The IPython.display module contains the Image function, which can display the results of a URL representing an image generated from a call to the Earth Engine getThumbUrl function. The following script will display a thumbnail of a global elevation model.
# Import libraries. import ee from IPython.display import Image # Trigger the authentication flow. ee.Authenticate() # Initialize the library. ee.Initialize() # Import a DEM and display a thumbnail of it. dem = ee.Image('USGS/SRTMGL1_003') Image(url=dem.updateMask(dem.gt(0)) .getThumbUrl())
Interactive map
The folium package can be used to display ee.Image objects on an interactive Leaflet map. Folium has no default method for handling tiles from Earth Engine, so one must be defined and added to the folium.Map module before use.
The following script provides an example of adding a method for handing Earth Engine tiles and using it to display an elevation model to a Leaflet map.
# Import libraries. import ee import folium # Trigger the authentication flow. ee.Authenticate() # Initialize the library. ee.Initialize() # Define a method for displaying Earth Engine image tiles to folium map. def add_ee_layer(self, ee_image_object, vis_params, name): map_id_dict = ee.Image(ee_image_object).getMapId(vis_params) folium.raster_layers.TileLayer( tiles = map_id_dict['tile_fetcher'].url_format, attr https://earthengine.google.com/">Google Earth Engine", name = name, overlay = True, control = True ).add_to(self) # Add EE drawing method to folium. folium.Map.add_ee_layer = add_ee_layer # Fetch an elevation model. dem = ee.Image('USGS/SRTMGL1_003') # Set visualization parameters. vis_params = < 'min': 0, 'max': 4000, 'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5']># Create a folium map object. my_map = folium.Map(location=[20, 0], zoom_start=3) # Add the elevation model to the map object. my_map.add_ee_layer(dem.updateMask(dem.gt(0)), vis_params, 'DEM') # Add a layer control panel to the map. my_map.add_child(folium.LayerControl()) # Display the map. display(my_map)
Chart visualization
Some Earth Engine functions produce tabular data that can be plotted by data visualization packages such as matplotlib . The following example demonstrates the display of tabular data from Earth Engine as a scatter plot. See Charting in Colaboratory for more information.
# Import libraries. import ee import matplotlib.pyplot as plt # Trigger the authentication flow. ee.Authenticate() # Initialize the Earth Engine module. ee.Initialize() # Fetch a Landsat image. img = ee.Image('LANDSAT/LT05/C01/T1_SR/LT05_034033_20000913') # Select Red and NIR bands, scale them, and sample 500 points. samp_fc = img.select(['B3','B4']).divide(10000).sample(scale=30, numPixels=500) # Arrange the sample as a list of lists. samp_dict = samp_fc.reduceColumns(ee.Reducer.toList().repeat(2), ['B3', 'B4']) samp_list = ee.List(samp_dict.get('list')) # Save server-side ee.List as a client-side Python list. samp_data = samp_list.getInfo() # Display a scatter plot of Red-NIR sample pairs using matplotlib. plt.scatter(samp_data[0], samp_data[1], alpha=0.2) plt.xlabel('Red', fontsize=12) plt.ylabel('NIR', fontsize=12) plt.show()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-02-14 UTC.