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.
- pip
- conda
pip3 install -U taospy
You'll need to have Python3 installed.
conda install -c conda-forge taospy
Config
Run this command in your terminal to save TDengine cloud token and URL as variables:
- Bash
- CMD
- Powershell
export TDENGINE_CLOUD_TOKEN="<token>"
export TDENGINE_CLOUD_URL="<url>"
set TDENGINE_CLOUD_TOKEN=<token>
set TDENGINE_CLOUD_URL=<url>
$env:TDENGINE_CLOUD_TOKEN='<token>'
$env:TDENGINE_CLOUD_URL='<url>'
Alternatively, you can also set environment variables in your IDE's run configurations.
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.
- REST
- WebSocket
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))
import taosws
import os
url = os.environ["TDENGINE_CLOUD_URL"]
token = os.environ["TDENGINE_CLOUD_TOKEN"]
try:
conn = taosws.connect("%s?token=%s" % (url, token))
except Exception as e:
print(str(e))
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
- conda
pip install jupyterlab
pip3 install -U taospy
You'll need to have Python3 installed.
conda install -c conda-forge jupyterlab
conda install -c conda-forge taospy
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))