Python Module sync_table Struggle

Hey, so I’m trying to just get to grips with the Python grist_api module but I’m struggling with sync_table. I’ve looked at the documentation but I don’t understand around key_cols and other_cols when it comes to “Each column in these lists must have the form (grist_col_id, new_data_col_id[, opt_type])”. Are you able to provide a basic example if a table just contains 2 columns, key and title?

First of all, it’s OK to struggle with this, it is pretty confusing and in need of updates/improvements.

key_cols and other_cols should be lists of tuples, where each tuple has two or three elements. The first element of each tuple is grist_col_id, a column ID of the table on the Grist side. The second element new_data_col_id is the name of the Python attribute used to access the corresponding value in your data in the client side.

Here’s an example as requested:

from collections import namedtuple

from grist_api import GristDocAPI

DOC_ID = "..."
TABLE_ID = "SyncExample"

api = GristDocAPI(DOC_ID)

# The data needs to be a list of objects with the right *attributes*,
# meaning values will be retrieved using `row.key` and `row.title`.
# You can't use dictionaries like `{"key": 1, "title": "World"}`
# because sync_table doesn't use `row["key"]`.
# So here we make a little class that has the right attributes. 
Row = namedtuple("Row", "key title")

data = [
  Row(key=2, title="World"),  # Row(2, "World") would also work
  Row(key=3, title="Goodbye"),
]

# Specify that the `key` column should be used for matching records,
# and set the values of the `title` column.
# Use the same column/attribute name on both sides to make things easier.
key_cols = [("key", "key")]
other_cols = [("title", "title")]

api.sync_table(TABLE_ID, data, key_cols, other_cols)

That transforms this data:

Screenshot from 2022-09-26 14-49-21

into this:

Screenshot from 2022-09-26 14-51-13

Row(key=2, title="World") led to an update, and Row(key=3, title="Goodbye") led to adding a new record.