Package: pyrtable
Pyrtable: Python framework for interfacing with AirtablePyrtable is a Python 3 library to interface withAirtable's REST API.There are other Python projects to deal with Airtable. However, most of them basically offer a thin layer to ease authentication and filtering – at the end, the programmer still has to manually deal with JSON encoding/decoding, pagination, request rate limits, and so on.Pyrtable is a high-level, ORM-like library that hides all these details. It performs automatic mapping between Airtable records and Python objects, allowing CRUD operations while aiming to be intuitive and fun. Programmers used toDjangowill find many similarities and will (hopefully) be able to interface with Airtable bases in just a couple of minutes.What does it look like?Ok, let's have a taste of how one can define a class that maps onto records of a table:importenumfrompyrtable.recordimportBaseRecordfrompyrtable.fieldsimportStringField,DateField,SingleSelectionField,\SingleRecordLinkField,MultipleRecordLinkFieldclassRole(enum.Enum):DEVELOPER='Developer'MANAGER='Manager'CEO='C.E.O.'classEmployeeRecord(BaseRecord):classMeta:# Open “Help > API documentation” in Airtable and search for a line# starting with “The ID of this base is XXX”.base_id='appABCDE12345'table_id='Employees'@classmethoddefget_api_key(cls):# The API Key can be generated in you Airtable Account page.# DO NOT COMMIT THIS STRING!return'keyABCDE12345'name=StringField('Name')birth_date=DateField('Birth date')office=SingleRecordLinkField('Office',linked_class='OfficeRecord')projects=MultipleRecordLinkField('Allocated in projects',linked_class='ProjectRecord')role=SingleSelectionField('Role',choices=Role)After that, common operations are pretty simple:# Iterating over all recordsforemployeeinEmployeeRecord.objects.all():print("%sis currently working on%dproject(s)"%(employee.name,len(employee.projects)))# FilteringforemployeeinEmployeeRecord.objects.filter(birth_date__gte=datetime.datetime(2001,1,1)):print("%swas born in this century!"%employee.name)# Creating, updating and deleting a recordnew_employee=EmployeeRecord(name='John Doe',birth_date=datetime.date(1980,5,10),role=Role.DEVELOPER)new_employee.save()new_employee.role=Role.MANAGERnew_employee.save()new_employee.delete()Notice that we don't deal with Airtable column or table names once record classes are defined.Beyond the basicsKeep in mind that Airtable isnota database system and is not really designed for tasks that need changing tons of data. In fact, only fetch (list) operations are batched – insert/update/delete operations are limited to a single record per request, and Airtable imposes a 5 requests per second limit even for paid accounts. You will need a full minute to update 300 records!That said, Pyrtable will respect that limit. In fact, it will track dirty fields to avoid unnecessary server requests and will render.save()calls as no-ops for unchanged objects. That also works with multiple threads, so the following pattern can be used to update and/or create several records:fromconcurrent.futures.threadimportThreadPoolExecutorall_records=list(EmployeeRecord.objects.all())# Do operations that change some records here# No need to keep track of which records were changedwithThreadPoolExecutor(max_workers=10)asexecutor:forrecordinall_records:executor.submit(record.save)Or, if you want a really nicetqdmprogress bar:fromtqdmimporttqdmwithThreadPoolExecutor(max_workers=10)asexecutor:for_intqdm(executor.map(lambdarecord:record.save(),all_records),total=len(all_records),dynamic_ncols=True,unit='',desc='Updating Airtable records'):passPyrtable also has some extra tools to cache data and to store authentication keys in JSON/YAML files or in an environment variable. Remember to never commit sensitive data to your repository, as Airtable authentication allowsfull R/W access to all your baseswith a single API Key!CompatibilityPyrtable is compatible with Python 3.8 and above. Python 2.x is not supported at all.DocumentationTechnical documentation is available athttps://pyrtable.readthedocs.io.Questions, bug reports, improvementsWant to try it out, contribute, suggest, offer a hand? Great! The project is available athttps://github.com/vilarneto/pyrtable.LicensePyrtable is released underMIT license.Copyright (c) 2020,2021,2022 by Vilar Fiuza da Camara Neto