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)