Jump to content
  • Write back to Google Sheets from Spotfire using Python Data Function


    This article explains how to write back to Google Sheets from Spotfire using a Python Data Function

    Introduction

    As the off-premise storage solutions grow, the need to write back to cloud systems is becoming more and more popular. In this example, we are going to write back to Google Sheets from Spotfire using a Python Data Function. We would need to set up the authentication part on the Google Cloud Platform for Spotfire to be able to write back. The same process goes when writing back to any other Google Cloud Platform system such as Google Big Query.

    gs.thumb.gif.90810fde2baf94ece7722727148bd60f.gif

    Setup Google Cloud Platform Authentication

    To set up the authentication, we need to create a project and create a service account that can perform calls to the Google Sheets API

    Create a new project

    A Google Cloud Platform Project consists of a set of users, APIs, billing, authentication, resources, and more. 

    Go to console.cloud.google.com, sign in with your Google account, and create a new project by clicking the top dropdown.

    newproject_0.png.e316e683d2a36da437ab9cb73b338acf.png

     

    2newproject.png.ec42f465b51fbad6e953bb43ff05e6cb.png

    Enable Google Sheets and Google Drive API

    An API is an Application Programming Interface. Google Cloud Platform has many of these, so we need to enable for this case the Google Drive and Google Sheets API.

    From the main menu on the left, choose APIs & Services, then select Enabled APIs & services and search for Google Sheets and Google Drive APIs

    apis1.png.85c01e786e346fbb8caade2942162c6f.png

     

    apis_1.png.f5cbb0b40c292f58dc9a47f788c11696.png

    Setup a service account

    A service account is a special type of account intended to represent a non-human user that needs to authenticate and be authorized to access Google APIs. Go to IAM & Admin to set up a service account. If not already done so, create a service account that will be working with this API through the + create credentials shortcut. You can always go to IAM & Admin - Service Accounts from the main menu and create one. Be careful about the role given for this account. The Owner role allows access to most Google Cloud resources. 

    auth.thumb.png.596afcd878b8af81b5f283bea84c8ebb.png

     

    auth2_0.png.ed1759e0191f193cd5c71bf26ddb89e9.png

    Create authentication keys

    Google Cloud APIs use the OAuth 2.0 protocol for authenticating both user accounts and service accounts. The OAuth 2.0 authentication process determines both the principal and the application. Most Google Cloud APIs also support anonymous access to public data using a GCP service account key. This key is used by the applications to make authorized API calls but in this exercise, we will be using authentication keys. 

    Form IAM & Admin - service Accounts, click on the service account you created and go to the keys tab, or go directly to Manage Keys from the Service account menu.  Create a new JSON key to download a JSON file containing the credentials. We are going to need the contents of this file later. Keep this service account password private, personal, and in a safe place!

    sak_1.thumb.png.bd0cd16751f50a52898d13b84d85edf9.png

    A service account JSON key file looks like this:

    {
      "type": "service_account",
      "project_id": "spotfire-cloud-writeback",
      "private_key_id": "jfielwssasdf99asdf02345d03453459dlldflsd",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQC7sd0q8HFh+I2T\nfpEN0pTMVRyTGWteZ/cSZR0uhMwIEUarlNWown9Y3QjLgxOYCkVGMO3726ewm3gm\nWxu4RZdDP984p6WQAsgPKbed+X3ahmrbmtxRBAEh5JZJgTeOl8wP/GG3/jsyEAvL\nr1WTO3Sk/ysRraOYq0KkMvI0VKTbOayPHuJ4QEe4AKlwTqZdYYtPU7Eq3+h+XNgY\nhB3L51mVe5f15/l/yfeS2YaAMiQqHypTDC6fvXPSkCBrv2A\nSssy0VNATwKBgEg5KZysUDKPinYMS950HBdqAaCB2WPd64Dh97hu05O0EUuqI0W4\nmwYhTJOaDqcmYiaqWc6d65nkvs/fjgquf9EmI+GTusT4uB/pM3XMKNQR6edNfNHE\nDT07cq3JUhNyMK+j2oXr+BLscqBhMK7vxUWNztbETDEDteorK3Y9dLAtAoGAauHh\n45CQNspuDixTiL4s3eI29yWOw03MNVFRA9sqktRh/Rwn4boCpb3DHkbg9gTzVVno\n8gv6KPqc35KrHsYxN4sV3oQuwvYhjTsdQp3wq3+qSGdMdqIC5Lu1rWpEzq94sm+W\n8VAzS6v1jXhwgjY2Btrs9Q9AG27s9klx6IAyuZUCgYB7GYPp08b+86wDoPggTNRi\nweE5eylpc+YBLFPZ0DCErjMyOctFFrHk8l+8T8OQzgpXO3udw3NYRH4qeYd3+ZN3\nSnuvaXjNzdht2olixeqqcadHX5CRngkmpKIT/EexkI+uLhvVgbAv/te+dklAbaSI\n5DxQKriHclJOAlui0XyNEg==\n-----END PRIVATE KEY-----\n",
      "client_email": "example@spotfire-cloud-writeback.iam.gserviceaccount.com",
      "client_id": "112233445566778899001",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth"",
      "token_uri": "https://oauth2.googleapis.com/token"",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs"",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/cacaca%40spotfire-cloud-writeback.iam.gserviceaccount.com""
    }
     

    Share a Google Sheet to GCP Service Account

    All is left from the Google side is to share the google sheet to the service account email. The service account email can be found on the Service Account JSON Key file. This Service Account looks like this: example@spotfire-cloud-writeback.iam.gserviceaccount.com

    gs.thumb.png.4a12239bbc51d3d1c61215580d9efe0f.png

    Configure Spotfire to Write back to Google Sheets

    Now that we have the authentication and Google Sheets in place, the final step is to create a Data Function to send data to Google Sheets. You can leave Google Sheets open and see how the data flows right after the data function exectues. No need to refresh the sheet every time. 

    Install python packages

    The required packages for the data function to work is oath2client and gspread. oath2client is a client library for OAuth2 delegates user authentication to the service that hosts a user account, allowing Spotfire to use the Google Sheets API. The gspread python package is a simple interface for working with Google Sheets

    These packages can be installed using Python Tools from the Spotfire client tools menu.

    ppack.png.e5de5eb8e9a4eb648504a8147bc7e839.png

    Register a new data function

    Data functions are the Spotfire way of letting advanced analysts, statisticians or mathematicians enhance Spotfire by creating scripts that can perform pretty much any type of calculation and returning the results to a Spotfire analysis

    In TIBCO Spotfire, on the menu bar, select Tools > Register data functions, select Python script and paste the code below to the script tab. Run the data funciton and check if the data function does not throw any errors. Check for errors with the notification icon. If there is some trouble with the loaded data, with a script or data function, or with a calculation in the analysis, a warning indication is displayed on the notifications icon on the menu bar.

    code1_1.png.64cd2f44bd664668a0e6359b23da4f84.png

    Edit Data Function

    The rest of the script performs authentication and inserts a row of data. Many other operation can be done with different methods but to keep things simple, the code has the minimal number of lines of code required to write back to Google sheets.

    Do not continue to edit the code but rather close the Regiter Data Function dialog. Click No to embed the data function in the analysis. You can always save it to the library later. 

    embed.png.55652e8b5a24672fadf99f54a876d144.png

    From Data > Data function properties select the script and edit by pasting the rest of the code. Just replace the keys variable with the content of the JSON key file

    from import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    import json
    
    scopes = [
    'https://www.googleapis.com/auth/spreadsheets';,
    'https://www.googleapis.com/auth/drive';
    ]
    
    keys = {
      "type": "service_account",
      "project_id": "spotfire-cloud-writeback",
      "private_key_id": "463a32...",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwxxx...",
      "client_email": "pythongsheets@spotfire-cloud-writeback.iam.gserviceaccount.com",
      "client_id": "112223334445556667778",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth"",
      "token_uri": "https://oauth2.googleapis.com/token"",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs"",
      "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/pythongsheets%40spotfire-cloud-writeback.iam.gserviceaccount.com""
    }
    import datetime
    tstamp = str(datetime.datetime.now())
    
    credentials = ServiceAccountCredentials.from_json_keyfile_dict(keys, scopes) 
    connection = gspread.authorize(credentials) 
    workbook = connection.open("spotfire comments") 
    sheet1 = workbook.get_worksheet(0) 
    sheet1.append_rows(values=[[tstamp, comment, value]])
     

    Configure Data Function parameters

    The script does not return any values at this point but input is required. The comment and value variables can be mapped to document properties

    params.png.3694fccd5acd3143b65607de2e1427f8.png

    Read back from Google Sheets

    The recommended way to read back from google sheets is using a connector. If a connector to read from Google Sheets is not available yet, you can use the following script to fetch the data as JSON or a Data Frame, which can be output to a document property or a Data Table respectively.

    import gspread
    from oauth2client.service_account import ServiceAccountCredentials
    import json
    import pandas as pd
    import numpy as np
    
    scopes = [
    'https://www.googleapis.com/auth/spreadsheets';,
    'https://www.googleapis.com/auth/drive';
    ]
    
    keys = {
      "type": "service_account",
      "project_id": "spotfire-cloud-writeback",
      "private_key_id": "463a3246b97bd9f364d9efb7d334df3a9fbf195d",
      "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggS...FAeAQ==\n-----END PRIVATE KEY-----\n",
      "client_email": "pythongsheets@spotfire-cloud-writeback.iam.gserviceaccount.com",
      "client_id": "112223344556677889900",
      "auth_uri": "https://accounts.google.com/o/oauth2/auth"",
      "token_uri": "https://oauth2.googleapis.com/token"",
      "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs"",
      "client_x509_cert_url": "https://www.googleapis.com/robo...teback.iam.gserviceaccount.com""
    }
    
    credentials = ServiceAccountCredentials.from_json_keyfile_dict(keys, scopes) 
    connection = gspread.authorize(credentials) 
    workbook = connection.open("spotfire comments") 
    worksheet = workbook.get_worksheet(0) 
    
    data = worksheet.get_all_values()
    
    #return as json 
    #output = (json.dumps(data, indent=2, sort_keys=False))
    
    #return as data frame
    output       = pd.DataFrame(np.array(data),columns=['date', 'comment','rating'])
     

    See also

    References

     

     


    User Feedback

    Recommended Comments

    There are no comments to display.


×
×
  • Create New...