Creating an iPhone App-Like Only With Your Data Science Skills: One-Tap Life Logger Linked to Google Sheets
By: Moto Dei
Tags: Heroku, Cloud Functions, Python, Data Science, iPhone

Demo 1 for API Solution — Google Cloud Functions

To have Google Cloud Functions receive the HTTP request and push record to Google Sheets, the followings are the steps:

Step 1 — Create a new GCP project

It is better to create a new one for isolation from other projects you already have. I created a new one with the name “Record iPhone Click”.

Step 2 — Configure Google Sheets first to receive the data push from Cloud Functions

Set up the Google Sheets side first because we need the authentication info for the Cloud Functions code.

Following the description in this page,

Enable “Google Drive API”. Enable “Google Sheets API”. Go to “APIs & Services > Credentials” and choose “Create credentials > Service account key”. Fill out the form Click “Create key” Select “JSON” and click “Create”. Download of a JSON file automatically takes place. Note the address from “client_email” in the JSON file. Save the JSON file to somewhere the Cloud Functions can access to. Create a spread sheet in Google Sheets. Write “event” in cell A1 and “time” in cell B1, as a header for the final log record. In the sheet file, pressing the “Share” button on the top right, add client_email you get from step 7 above as an authorized user.

This is all!

Step 3— Configure a new function in Cloud Functions

Go to “Cloud Functions” and CREATE FUNCTION. Use Trigger type as “HTTP”.

Configuration of new Cloud Functions (snapped by author)

Step 4— Input code to main.py and requirements.txt

Press Next on the bottom left, and go to the code input page.

Code of new Cloud Functions

Here’re the code I actually used.

main.py

requirements.txt

Just remember that the service account JSON file is required to be downloaded into local folder and when you want to do it in Cloud Functions, you have to save the file in /tmp/ sub-folder. Otherwise, Cloud Functions refuses to build your app.

Step 5 — Deploy the function

Click “Deploy” button and wait for the successful deployment. It can take a few minutes.

Step 6 — Find the URL and check if it’s working

URL can be found in the details of the function.

Where you can find the URL.

If you test-click it, you will see the following-like message, which means the code is successfully deployed. If you see internal error, server error, or any other errors, something is wrong.

Successful message.

Here you should also see a new record in the sheet you created.

Newly inserted record in the sheet.

Step 7 — Send the URL to your iPhone and make a Safari link on your home screen

Open your URL on your iPhone and add the shortcut to Home Screen.

Add your link to Home Screen

Then, you can see the new icon on your iPhone home screen.

Done!

What we finally got

Step 8 — To make the URL links for your other life logs, copy the function again and again, set up the event name, and add each shortcut to your home screen.

This is the most inconvenient part of this option: Cloud Function’s code can be only linked to one HTTP request URL. Therefore, if you need more than one life logs (like “feed baby”, “baby wakes up”, “arrived at office”, etc.), only you can do is to create the functions again and again.

Make separate functions to every event.

This process can be significantly reduced when you have more control in API set up like the next option using Heroku!