DEV Community

Cover image for Using Google Cloud from Colab
Laurent Picard for Google Cloud

Posted on • Edited on • Originally published at Medium

Using Google Cloud from Colab

Google Colab is an amazing tool for Pythonistas. It can be used for a variety of tasks, from quickly experimenting with Python code to sharing extensive data processing notebooks with the world. Colab runs on Google Cloud, which gives you a boost when accessing cloud services because your code is running inside Googleโ€™s high performance network, and also offers a simple way to use Google Cloud services, letting you run powerful cloud workloads from your browser.


๐Ÿ“ฆ๏ธ Install dependencies

Colab comes with hundreds of preinstalled packages, including pandas, numpy, matplotlib, Flask, Pillow, tensorflow, and pytorch. You can install additional required dependencies as needed.

For example, to analyze text with the power of machine learning using the Natural Language API, install the google-cloud-language client library:

!pip install google-cloud-language>=2.9.1
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”‘ Authenticate

To authenticate with your Google Cloud account within the Colab notebook, use the authenticate_user method. A new parameter lets you specify your project ID:

from google.colab import auth

PROJECT_ID = ""  # @param {type:"string"}

auth.authenticate_user(project_id=PROJECT_ID)
Enter fullscreen mode Exit fullscreen mode

After this step:

  • ๐Ÿ‘ You are authenticated for gcloud CLI commands.
  • ๐Ÿ‘ You are also authenticated when using Google Cloud Python client libraries. Note that you generally wonโ€™t need to create a service account.
  • ๐Ÿ‘ Your default project is set.
  • ๐Ÿ‘ Your notebook can also access your Google Drive, letting you ingest or generate your own files.

๐Ÿ”“ Enable APIs

Ensure required APIs are enabled. In our example, thatโ€™s the service language.googleapis.com:

!gcloud services enable language.googleapis.com
Enter fullscreen mode Exit fullscreen mode

๐Ÿคฏ Use Google Cloud services

Thatโ€™s it! You can now directly use the service by calling its Python client library:

from google.cloud import language

def analyze_text_sentiment(text: str) -> language.AnalyzeSentimentResponse:
    client = language.LanguageServiceClient()
    document = language.Document(
        content=text,
        type_=language.Document.Type.PLAIN_TEXT,
    )
    return client.analyze_sentiment(document=document)

# Input
text = "Python is a very readable language, ..."  # @param {type:"string"}

# Send a request to the API
response = analyze_text_sentiment(text)

# Use the results
print(response)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Š Benefit from notebook advanced features

Colab is hosting a Jupyter notebook. I personally depict Colab as the โ€œGoogle Drive of notebooksโ€. As notebooks have additional superpowers, this lets you nicely process and visualize your data, such as tables, images, or charts.

In our example, the function show_text_sentiment gathers results in a pandas DataFrame, which renders as a table:

Screencast showing successive text sentiment analyses with results displayed in a table


โœจ Check it out

In these examples, click the โ€œOpen in Colabโ€ link:

๐Ÿ’ก You can directly create a new notebook by opening the colab.new URL.


๐Ÿ‘‹ Have fun!

Top comments (0)