I’m making a survey for my company, and want all the users to input their company username, which is unique to every user. In order to avoid errors, I made it a drop down that references a Coda table with all the acceptable usernames in the company (~6,000). That list obviously changes quite often, and it’s source is in our Redshift DataWarehouse. I want to refresh that list programmatically, but using Python it looks like I have to delete it row by row and then add rows in batches of 10… by the time it’s done it’ll be time to run it again!
Is there an easier way to do data validation in my survey? Or is there a faster way to drop and refill a table?
PS, I’ve tried collecting the submitter’s email in the form because I could usually extract their username from it eg. ‘sthompson@mycompany.com’, but occasionally people have customized their email so it’s not a perfect match.
# Delete rows from coda table
def delete_rows(coda, doc_id, table_id):
# Read in current row IDs
rows = coda.list_rows(doc_id = doc_id, table_id_or_name = table_id)
# Extract table values
table_values = rows['items']
# Convert table values into dataframe
output = pd.DataFrame()
for i in range(len(table_values)):
row = {k: table_values[i][k] for k in ('id', 'index', 'createdAt', 'updatedAt', 'browserLink')}
values = table_values[i]['values']
row.update(values)
output = pd.concat([output, pd.DataFrame(row, index=[0])], ignore_index=True)
# Delete rows
for i in range(output.shape[0]):
coda.delete_row(
doc_id=doc_id,
table_id_or_name=table_id,
row_id_or_name=output['id'][i]
)
# Introduce a delay between row deletions to respect rate limits
time.sleep(2) # Adjust the delay as needed
# Generate Coda-friendly payload
def generate_payload(df, columns):
import json
from collections import defaultdict
payload = defaultdict(list)
row_array = defaultdict(list)
for idx, row in df.iterrows():
for col in columns:
d = {'column': col, 'value': str(row[col])}
row_array['cells'].append(d)
payload['rows'].append(row_array)
row_array = defaultdict(list)
return json.dumps(payload, indent=2)
# Upload to coda
def coda_upload(catalog, token, doc_id, table_id):
import json
# Connect to Coda
coda = coda_connect(token)
# Delete rows
delete_rows(coda, doc_id, table_id)
# Generate payload
payload = generate_payload(catalog, list(catalog.columns))
payload = json.loads(payload)
# Upsert rows in batches
batch_size = 10 # Number of rows to include in each batch
total_rows = len(payload['rows'])
for i in range(0, total_rows, batch_size):
batch_payload = {
'rows': payload['rows'][i:i+batch_size]
}
coda.upsert_row(doc_id=doc_id, table_id_or_name=table_id, data=batch_payload)
# Introduce a delay between batches to respect rate limits
time.sleep(3)