Skip to main content

Connect with Python

Install Client Library

First, you need to install the taospy module version >= 2.6.2. Run the command below in your terminal.

pip3 install -U taospy

You'll need to have Python3 installed.

Config

Run this command in your terminal to save TDengine cloud token and URL as variables:

export TDENGINE_CLOUD_TOKEN="<token>"
export TDENGINE_CLOUD_URL="<url>"

Alternatively, you can also set environment variables in your IDE's run configurations.

IMPORTANT

Replace <token> and <url> with cloud token and URL. To obtain the value of cloud token and URL, please login TDengine Cloud and click "Programming" on the left menu, then select "Python".

Connect

Copy code bellow to your editor, then run it. If you are using jupyter, assuming you have followed the guide about Jupyter, you can copy the code into Jupyter editor in your browser.

import taosrest
import os

url = os.environ["TDENGINE_CLOUD_URL"]
token = os.environ["TDENGINE_CLOUD_TOKEN"]

try:
conn = taosrest.connect(url=url, token=token)
# test the connection by getting version info
print("TDengine version: ", conn.server_info)
except Exception as e:
print(str(e))

view source code

For how to write data and query data, please refer to Data In and Tools.

For more details about how to write or query data via REST API, please check REST API.

Jupyter

Step 1: Install

For the users who are familiar with Jupyter to program in Python, both TDengine Python client library and Jupyter need to be ready in your environment. If you have not done yet, please use the commands below to install them.

pip install jupyterlab
pip3 install -U taospy

You'll need to have Python3 installed.

Step 2: Configure

In order for Jupyter to connect to TDengine cloud service, before launching Jupyter, the environment setting must be performed. We use Linux bash as example.

export TDENGINE_CLOUD_TOKEN="<token>"
export TDENGINE_CLOUD_URL="<url>"
jupyter lab

Step 3: Connect

Once jupyter lab is launched, Jupyter lab service is automatically connected and shown in your browser. You can create a new notebook and copy the sample code below and run it.

import taosrest
import os

url = os.environ["TDENGINE_CLOUD_URL"]
token = os.environ["TDENGINE_CLOUD_TOKEN"]

try:
conn = taosrest.connect(url=url, token=token)
# test the connection by getting version info
print("TDengine version: ", conn.server_info)
except Exception as e:
print(str(e))

view source code