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
๐ 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)
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
๐คฏ 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)
๐ 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:
โจ Check it out
In these examples, click the โOpen in Colabโ link:
- Using Google Cloud from Colab (short)
- Using the Natural Language API with Python (detailed)
๐ก You can directly create a new notebook by opening the colab.new URL.
๐ Have fun!
- Read the Google Colab blog to learn more about Colab.
- Follow me (Twitter / LinkedIn) for more cloud explorations ;)
Top comments (0)