Using CloudTables with Python

Author: Colin Marks 14th June 2022

Introduction

We have recently added a Python client for the CloudTables API. This blog post, along with this video, will walk you through the steps on how to install and use it. The Python API is fully documented, with all methods and parameters listed. Read on for more details, or if you prefer a video format, see below:

Installation

The CloudTables library is available as the CloudTablesApi package on PyPi and can be installed for your project using:

# pip
pip install CloudTablesApi
# python
python3 -m pip uninstall CloudTablesApi

Constructor

The CloudTables API library provides a class whose constructor returns an API instance. First include the API in your project, then initialize the API:

from CloudTablesApi import CloudTablesApi

api = CloudTablesApi('{apiKey}', subdomain='{subdomain}')

where:

  • {apiKey} is an API key for your application (see the Security sidebar in your app)
  • {subdomain} is the part of your apps URL, i.e. subdomain.cloudtables.com

Dataset methods

Now you've got your API instance, you can use that to query your application. Please see below for some examples:

from CloudTablesApi import CloudTablesApi

api = CloudTablesApi('{apiKey}', subdomain='{subdomain}')

# Display the application's datasets
datasets = api.datasets()
print(datasets)

# Get the data for a specific dataset
data = api.dataset({data-set-id}).data()
print(data)

# Add a record to a specific dataset
newrecord = api.dataset({data-set-id}).insert({'dp-1': 'aaa', 'dp-2': 5, 'l-1': 1})
print(newrecord)

Note

  • the {data-set-id} needs to be replaced with the ID of your data set - this can be found in the Data Set Inspector, on the Data tab for your data set.
  • on the insert, the data point IDs will be specific to your data points - the IDs for yours can be found on the data sets Data tab

Embedding a CloudTable

The API can also be used to get a script tag to embed the CloudTable into your web page. This is secured by a token. The token ties that session to the web browser session - so if your network traffic was sniffed, hackers couldn't access the CloudTable or its data because that token is already tied to the original browser session.

from CloudTablesApi import CloudTablesApi

api = CloudTablesApi('{apiKey}', subdomain='{subdomain}')

token = api.token()
scripttag = api.dataset({data-set-id}).script_tag(token['token'], style='bs5')

Summary

As you've seen, it's very easy to securely embed a CloudTable in a Python application. I hope this has been useful - don't hesitate to get in touch if you have any questions or comments!

As always, please let us know if you'd like a topic discussed in a blog post, we're keen to tailor this to the needs of the community.