Python Pandas Dataframe to Google Sheets for Tableau Public LIVE | by Eklavya Saxena | Towards Data Science

This blog is a part of Automated ETL for LIVE Tableau Public Visualizations and is sub-divided into three parts, namely:
Enable Google Sheets API and Create CredentialsConnect Python to Google Sheets and Export the DataframeEnable Tableau Public LIVE using Google Sheets
Enable Google Sheets API and Create Credentials
Connect Python to Google Sheets and Export the Dataframe
Enable Tableau Public LIVE using Google Sheets
Accessing your Google API Console is the first step to connect python scripts with google sheets. Step wise follow along to enable google sheets API and create required credentials.
On the top left corner, click on the drop-down ‘Select a project’, which will open a pop-up windowOn the top right corner of this pop-up, click on ‘NEW PROJECT’, which will take you to a new windowGive your project a suitable name (for e.g: covid-viz-data). You may leave the ‘Location *’ field as it is and click on ‘CREATE’
On the top left corner, click on the drop-down ‘Select a project’, which will open a pop-up window
On the top right corner of this pop-up, click on ‘NEW PROJECT’, which will take you to a new window
Give your project a suitable name (for e.g: covid-viz-data). You may leave the ‘Location *’ field as it is and click on ‘CREATE’
Select ‘Library’ (under ‘Dashboard’) from the left pane, which will re-direct you to ‘API Library’ window an option to search for APIs and servicesType in the field ‘Google Sheets API’ and select it from the result, which will re-direct you the Google Sheets API windowClick on ‘ENABLE’ to enable it for your project created in the previous step
Select ‘Library’ (under ‘Dashboard’) from the left pane, which will re-direct you to ‘API Library’ window an option to search for APIs and services
Type in the field ‘Google Sheets API’ and select it from the result, which will re-direct you the Google Sheets API window
Click on ‘ENABLE’ to enable it for your project created in the previous step
Similarly, enable Google Drive API as python library imported later may use the functionalities of both the above APIs.
You may select ‘CREATE CREDENTIALS’ on the right side of the Google Sheets API window or alternatively if you are on dashboard — start with Step 1.
Select ‘Credentials’ (under ‘Library’) from the left pane. This will display you the ‘Credentials’ pane on the rightClick on the ‘CREATE CREDENTIALS’ option on the top, which will display a drop-down menuClick on ‘Help me choose’ option from the drop-down menu, which will redirect you to a pane with heading ‘Add credentials to your project’On this ‘Add credentials to your project’ pane follow along:Step 1: ‘Find out what kind of credentials you need’Which API are you using? — Google Sheets APIWhere will you be calling the API from? — Web serverWhat data will you be accessing? — Application dataAre you planning to use this API with App Engine or Compute Engine? — No, I’m not using themNow, click on ‘What credentials do I need?’ for suggestion as per our inputStep 2: ‘Create a service account’Service account name — (for e.g: covid-data-update)Role — Editor (From drop-down select ‘Project’ and then ‘Editor’)Service account ID — auto-createdKey type — JSONNow, click on ‘Continue’.This will ask you to save credentials .json file which allows access to your cloud resources — so store it securely.
Select ‘Credentials’ (under ‘Library’) from the left pane. This will display you the ‘Credentials’ pane on the right
Click on the ‘CREATE CREDENTIALS’ option on the top, which will display a drop-down menu
Click on ‘Help me choose’ option from the drop-down menu, which will redirect you to a pane with heading ‘Add credentials to your project’
On this ‘Add credentials to your project’ pane follow along:Step 1: ‘Find out what kind of credentials you need’Which API are you using? — Google Sheets APIWhere will you be calling the API from? — Web serverWhat data will you be accessing? — Application dataAre you planning to use this API with App Engine or Compute Engine? — No, I’m not using themNow, click on ‘What credentials do I need?’ for suggestion as per our inputStep 2: ‘Create a service account’Service account name — (for e.g: covid-data-update)Role — Editor (From drop-down select ‘Project’ and then ‘Editor’)Service account ID — auto-createdKey type — JSONNow, click on ‘Continue’.This will ask you to save credentials .json file which allows access to your cloud resources — so store it securely.
Now that we have our secret credentials to access the API (saved in .json format), we need to make sure that the target google sheet is shared with the client email generated in that .json file.
Copy the email address value from ‘client_email’ key in the .json fileCreate a new google sheet, where you want to export your dataOnce the sheet is opened, click ‘Share’ on the top right cornerPaste the copied email address and give it ‘Can edit’ access
Copy the email address value from ‘client_email’ key in the .json file
Create a new google sheet, where you want to export your data
Once the sheet is opened, click ‘Share’ on the top right corner
Paste the copied email address and give it ‘Can edit’ access
Congratulations! we have successfully enabled Google Sheets API from Google API Console and created credentials to access this API via python script. Now, let’s move onto to the second part.
While Google created the Google Sheets API, they, of course, did not stay behind to beautifully create a guide for Python QuickStart. I would highly recommend to referring it as the additional documentation/guide enables vast possibilities to play around with Google Sheets.
Why this blog then?The quick-start asks you to install 3 libraries, namely google-api-python-client, google-auth-httplib2, google-auth-oauthlib.But, as evident from the widespread plethora of python libraries — here we have:
pygsheets — A simple, intuitive python library to access google spreadsheets through the Google Sheets API v4
This part will utilize pyghseets to export Dataframe to Google Sheets.Let’s get started:
import sys!{sys.executable} -m pip install pygsheets
Why I did not use !pip install pyghseets ?In Jupyter, the above code makes sure that you are running the pip version associated with the current Python kernel. Also, FYI the shell environment and the Python executable are disconnected. Click here for more information.
import pygsheetsclient = pygsheets.authorize(service_file='/Users/eklav/credentials.json')print("-----------------Authorized--------------------")
After installation, the next step is to of course import the library. Then, create a variable client using authorize functionality to authenticate python script (or an application) with a google account.
sheet = client.open('COVID-19')print("-----------------Sheet Opened------------------")
Now, use open functionality to return the spreadsheet (created in the previous part) by title (e.g: ‘COVID-19’) and assign it to the variable sheet .
wks = sheet[0]print("-----------------First Sheet Accessed----------")
To access the first sheet, worksheet on ‘0th’ index of sheet is assigned to the variable wks .
wks.set_dataframe(df_COVID,(1,1))print("-----------------Data Updated------------------")
Now, use .set_dataframe functionality of worksheet wks to assign values ‘df_COVID’* and ‘(1,1)’ to ‘df’ and ‘start’ parameters respectively.The value ‘(1,1)’ refers to the A1 cell of the spreadsheet.
To know how dataframe ‘df_COVID’ was curated, please refer to the first part — ‘Data Extraction from GitHub and Auto-run or Schedule Python Script’ of the blog Automated ETL for LIVE Tableau Public Visualizations.
This brings us to the end of this part. We have enabled APIs and credentials using which we connected Python to Google Sheets — your free cloud storage.
As mentioned in the parent blog, finally it is time to bust the myth that Tableau Public cannot have LIVE data. By now, we have a Python script being run automatically by Windows Task Scheduler, which in turns updates the Google Sheets. And this sheet will be our source of data for dashboard or visualizations published over Tableau Public.
I will refrain from elaborating on a very well-known step — to select a data source from the Tableau Start Page. If required, please follow the GIF below:
Well, now you may go ahead and create visualizations as required with data sourced LIVE from your Google Sheets
While you have a LIVE connection with Google Sheet, it must be straight forward to publish the workbook. Just connect to https://public.tableau.com server by logging in and share.
But, here is the catch! The following error will pop-up:
“The Tableau server you are publishing to requires extracts to be enabled for data sources. Use the Data menu to enable the extracts for the following data sources:
Data Extract Required”
Now the question is — Data Extract???
The whole point of sourcing data from Google Sheets was to have a LIVE connection. And, this is where we believe that Tableau Public just works with extracts.
But, Tableau has more to serve — without a blink of an eye just go ahead and make your connection to ‘Extract’. Now when you share, tick the checkbox which displays the following and save:
“Keep my data in sync with Google Sheets and embed my Google credentials”
Once ‘Save’ button is clicked, you will be re-directed to Tableau Public, with your dashboard published. Scroll down and notice the ‘Request Update’ button. This button is to force update the dashboard accessing the latest data from the Google Sheets. Else otherwise, what I have experienced Tableau refreshes the dashboard automatically every day.
Bingo!
Introduction to the Google Sheets API
Reading and Writing to Google Spreadsheets using Python by Erik Rood
Documentation on pygsheets Python Library
Thank you for reading! I hope this blog revealed an interesting aspect of Tableau Public. Go ahead and make your visualizations LIVE. If this article was helpful, share it.
Let me know in a comment:
If you felt like this did or didn’t help, orIf you would like to know ‘how to’ of different features used in Tableau visualizations
If you felt like this did or didn’t help, or
If you would like to know ‘how to’ of different features used in Tableau visualizations