Skip to content

SeaTable Python script

The Python script runs on the server side and can be set to automatically run periodically, which is suitable for more complex data processing scenarios.

Python scripts can be run on your local machine or uploaded to the SeaTable cloud to run. Local operation is convenient for development and debugging, and scripts can be easily integrated into larger projects.

How to make the script support both local and cloud run

When the script runs in the cloud, it will provide a context object, which contains the server URL auto generated by the system and the API token of base. If you run the script in local, you need to manually specify these two variables; the API token can be generated in the drop-down menu "Advanced -> API Token" of the table.

Use the following method to make the script support both local and cloud run

from seatable_api import Base, context

server_url = context.server_url or 'https://cloud.seatable.io'
api_token = context.api_token or 'c3c75dca2c369849455a39f4436147639cf02b2d'


base = Base(api_token, server_url)
base.auth()

Dependencies that need to be installed to run the script local

The script need to install seatable-api when run in local.

pip3 install seatable-api

Requirements

  • Python >= 3.5
  • requests
  • socketIO-client-nexus

A simple example

This following example shows how to operate records in a table.

base = Base(api_token, server_url)
base.auth()

rows = base.list_rows("Table1")

row_data = {'name': 'Tom', 'age': 18}
base.append_row('Table1', row_data)
base.update_row('Table1', 'U_eTV7mDSmSd-K2P535Wzw', row_data)
base.delete_row('Table1', 'U_eTV7mDSmSd-K2P535Wzw')

How to monitor base changes when you run script locally

You can run monitor base changes using socketIO as following:

from seatable_api import Base
from seatable_api.constants import UPDATE_DTABLE

server_url = 'https://cloud.seatable.io/'
api_token = 'xxxxxx'

base = Base(api_token, server_url)
base.auth(with_socket_io=True)

# You can overwrite this event

def on_update_seatable(data, index, *args):
    print(data)

base.socketIO.on(UPDATE_DTABLE, on_update_seatable)
base.socketIO.wait()  # forever

Reference

Data structure of object in SeaTable:

SeaTable API introduction:

Example

You can find some easy to understand examples through this linkhttps://github.com/seatable/seatable-scripts/tree/master/examples/python

Detail as follow

  • send_email.py: Read pictures/files in one table as attachments and send email to contacts in another table
  • generate_barcode.py: Transfer the text into barcode and save it into image type column of a base