diff --git a/docs/en/10-programming/01-connect/01-python.md b/docs/en/10-programming/01-connect/01-python.md index f216a0a1ba26f7a3dbc1073a724c116086c15fbb..0687a5bbf1e97eeb32ce938a8ea1ef31d202c045 100644 --- a/docs/en/10-programming/01-connect/01-python.md +++ b/docs/en/10-programming/01-connect/01-python.md @@ -78,10 +78,23 @@ To obtain the value of cloud token and URL, please log in [TDengine Cloud](https 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. + + + ```python {{#include docs/examples/python/develop_tutorial.py:connect}} ``` + + + +```python +{{#include docs/examples/python/develop_tutorial_ws.py:connect}} +``` + + + + For how to write data and query data, please refer to [Data In](https://docs.tdengine.com/cloud/data-in/) and [Data Out](https://docs.tdengine.com/cloud/data-out/). For more details about how to write or query data via REST API, please check [REST API](https://docs.tdengine.com/cloud/programming/connector/rest-api/). diff --git a/docs/examples/python/develop_tutorial_ws.py b/docs/examples/python/develop_tutorial_ws.py new file mode 100644 index 0000000000000000000000000000000000000000..d665ba4bb1240bd7710637e9245d9d2357fa592a --- /dev/null +++ b/docs/examples/python/develop_tutorial_ws.py @@ -0,0 +1,45 @@ +from dotenv import load_dotenv + +# read .env file from current working directory +load_dotenv() + +# ANCHOR: connect +import taosws +import os + +url = os.environ["TDENGINE_CLOUD_URL"] +token = os.environ["TDENGINE_CLOUD_TOKEN"] + +try: + conn = taosrest.connect("%s?token=%s" % (url, token)) +except Exception as e: + print(str(e)) +# ANCHOR_END: connect +# ANCHOR: insert +# create super table +conn.execute("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)") +# insert multiple rows into multiple tables at once. subtables will be created automatically. +affected_row = conn.execute("""INSERT INTO power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) + power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) + """) +print("affected_row", affected_row) # 4 +# ANCHOR_END: insert +# ANCHOR: query +result = conn.query("SELECT ts, current FROM power.meters LIMIT 2") +# ANCHOR_END: query + +# ANCHOR: fields +print(result.fields) +# output: [{'name': 'ts', 'type': 9, 'bytes': 8}, {'name': 'current', 'type': 6, 'bytes': 4}] +# ANCHOR_END: fields + +# ANCHOR: rows +print(result.rows) +# output: 3 +# ANCHOR_END: rows +# ANCHOR: iter +for row in result: + print(row) +# output: +# [datetime.datetime(2018, 10, 3, 14, 38, 5, tzinfo=datetime.timezone.utc), 10.3] +# [datetime.datetime(2018, 10, 3, 14, 38, 15, tzinfo=datetime.timezone.utc), 12.6] diff --git a/docs/zh/10-programming/01-connect/01-python.md b/docs/zh/10-programming/01-connect/01-python.md index 4f5d0baf4223c2725400cf9158e658ad21ecefa1..240f48ffc4ad13fabfb4d60464c28b4859b95019 100644 --- a/docs/zh/10-programming/01-connect/01-python.md +++ b/docs/zh/10-programming/01-connect/01-python.md @@ -76,10 +76,23 @@ $env:TDENGINE_CLOUD_URL='' 复制下面的代码到您的编辑器,然后执行这段代码。如果您正在使用 Jupyter 并且按照它的指南搭建好环境,您可以负责下面代码到您浏览器的 Jupyter 编辑器。 + + + ```python {{#include docs/examples/python/develop_tutorial.py:connect}} ``` + + + +```python +{{#include docs/examples/python/develop_tutorial_ws.py:connect}} +``` + + + + 对于如何写入数据和查询输入,请参考。 想知道更多通过 REST 接口写入数据的详情,请参考[REST 接口](https://docs.taosdata.com/cloud/programming/connector/rest-api/).