DEV Community

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

Posted on • Updated 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)