This python tutorial is relevant to coaching how to automate your Python applications with the Google cloud platform. Compared to local device crontab and cron jobs, Cloud platforms like Google can allow your application 24 hours and 7 days stand-by and run by triggering preset events. You need to either keep the laptop running 24 hours or sit beside the device to monitor. I will take the trending topic bot for example and walk you through all components and script codings.
Platform & Python module: Google Cloud platform, google news, JSON, pandas, DateTime, gspread
- How does it work and why do we need it?
- Sign up for a Google Cloud Account and Create a Billing Account
- Deploy Google Cloud Function
- Set up Cloud Scheduler Connecting with the Specific Cloud Function Topics
- Full Python Application Scripts that can be enhanced with the automation capability
Google Cloud Platform – How does it work and why do we need it?
Basically, the Google Cloud Platform allows us to deploy a Python script to run automatically based on the schedule and workflow settings. Depending on the work volume, it can either be free or not. And even if you need to pay for some excess volume, it’s the pay-as-you-go model.
Python automation has great business and marketing value. It’s because it makes it possible to real-time optimize the advertising campaign, update the dashboard to grab intelligent data, and refresh the latest trending content automatically. Previously I tried to write some articles regarding APIs, price trackers, and trending topics. If you are interested in some application scenarios, please feel free to check them out
Sign up for a Google Cloud Account and Create a Billing Account
Compared to the Google APIs console account, we need to add the credit card to sign up for a Google cloud account, although both are in the same interface. It’s because Google needs us to enable the billing account for Cloud function, App engine, or computer engine. If your usage exceeds the free tier, GCP would automatically charge you based on the actual volume. And as well as activating Google APIs, we need to create a project as well using the account with a billing account.
Cloud Functions provides a perpetual free tier for compute-time resources, which includes an allocation of both GB-seconds and GHz-seconds. In addition to the 2 million invocations, the free tier provides 400,000 GB seconds, 200,000 GHz seconds of computing time, and 5GB of Internet egress traffic per month.
The free tier is measured as a dollar equivalent to the Tier 1 pricing. The equivalent dollar amount is provided whether you run your functions in a region with Tier 1 pricing or Tier 2 pricing or both. However, the deduction of usage of the free tier will be based on whether the region in which the function is running is in Tier 1 or Tier 2
Google Cloud Platform – Configure, Deploy and Set up Google Cloud Function
The most complex step is the Python script you are going to upload to GCP, and in terms of the function, deployment is not difficult. There are basically 4 steps.
Create a Function
Here we need to select the server we’re going to store our application and scripts and the methods of triggers. There are several options that we could select based on the project needs. In terms of operational automation and marketing automation, Cloud Pub/Sub can be the best one. It’s because it’s much easier to deploy with all user authentication done internally, so the credentials are not necessary.
Set up Runtime Environment Variables in the Function Section
In a Python script, it might include and involve some sensitive data and numbers, such as the password, etc. This section called runtime environment variables gives people to create some variables that have the sensitive data value instead of putting them onto the script directly.
Upload the Python Script and Requirement TXT
In the Cloud function script interface, Google sets to start with a def with two arguments. It’s mandatory whether you use them or not. If you don’t have any arguments, just leave them both there as what it is.
The other thing you need to be aware of is the entry point name must be the same as your function name. For example, here I name it the trendingNews2022, and the def function name is also the same.
Then, we need to tell Google what Python modules the script requires. So we can add the module and version bylines in the requirement.txt.
Deploy and Test the Script
When things are ready, we can click the deploy button, and Google would take some time to process. When you see a green light turning on on the left-hand side of your function. That means the script doesn’t have outstanding issues at the moment.
However, the green light doesn’t mean that your automation is working properly and correctly. We need to test the function and check the logs and see if there are some bugs or something else before launching.
Google Cloud Platform – Set up Cloud Scheduler Connecting with the Specific Cloud Function Topics
We have finished the most difficult part, but it seems it’s not that difficult in fact. Here we need to create a cloud schedule and connect the function topic we just created.
The thing we need to do here is to set up the frequency using the cron methodology. I’m not going to go into details in this section as I have another article relevant to this topic. If you are interested, please check out this.
Full Python Application Scripts that can be enhanced with the automation capability
If you are interested in the full python Tutorial 52 – Utilise Google Cloud Platform to Implement Python Script Automation with Cloud Function and Cloud Scheduler, please subscribe to our newsletter by adding the message “Python Tutorial 53”. We would send you the script immediately to your mailbox.
I hope you enjoy reading Python Tutorial 52 – Utilise Google Cloud Platform to Implement Python Script Automation with Cloud Function and Cloud Scheduler. If you did, please support us by doing one of the things listed below, because it always helps out our channel.
- Support my channel through PayPal (paypal.me/Easy2digital)
- Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
- Follow and like my page Easy2Digital Facebook page
- Share the article to your social network with the hashtag #easy2digital
- Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
- You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software
- Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)