Using Google Cloud from Colab
Tags: googlecloud, googlecolab, python, notebook
Published: 2023-06-16 08:49:26

---
title: "Using Google Cloud from Colab"
published: true
description: "Colab is a great tool for Pythonistas. It can be used for a variety of tasks and also offers a simple way to use Google Cloud services."
tags: googlecloud,googlecolab,python,notebook
cover_image: https://miro.medium.com/v2/resize:fit:2000/format:webp/1*nLjcqvMkvZ5TRHF4pZjbHg.gif
canonical_url: https://medium.com/google-cloud/using-google-cloud-from-colab-75691f4a731?source=friends_link&sk=1045f8cdd0b4b494fc2e1ef51edf1b6d
---

[Google Colab](https://colab.research.google.com) 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](https://cloud.google.com/natural-language/docs), install the `google-cloud-language` client library:

```python
!pip install google-cloud-language>=2.9.1
```

---

## 🔑 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:

```python
from google.colab import auth

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

auth.authenticate_user(project_id=PROJECT_ID)
```

After this step:

- 👍 You are authenticated for `gcloud` [CLI commands](https://cloud.google.com/sdk/gcloud/reference).
- 👍 You are also authenticated when using Google Cloud [Python client libraries](https://cloud.google.com/python/docs/reference). 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`:

```python
!gcloud services enable language.googleapis.com
```

---

## 🤯 Use Google Cloud services

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

```python
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)
```

---

## 📊 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97z7jyabkpmwbndndgl2.gif)

---

## ✨ Check it out

In these examples, click the “_Open in Colab_” link:

- [Using Google Cloud from Colab](https://github.com/GoogleCloudPlatform/devrel-demos/blob/main/other/colab/Using%20Google%20Cloud%20from%20Colab.ipynb) (short)
- [Using the Natural Language API with Python](https://github.com/GoogleCloudPlatform/devrel-demos/blob/main/other/colab/Using%20the%20Natural%20Language%20API%20with%20Python.ipynb) (detailed)

> 💡 You can directly create a new notebook by opening the [colab.new](https://colab.new) URL.

---

## 👋 Have fun!

- Read the [Google Colab](https://medium.com/google-colab) blog to learn more about Colab.
- Follow me ([Twitter](https://twitter.com/PicardParis) / [LinkedIn](https://linkedin.com/in/PicardParis)) for more cloud explorations ;)