pyairtable - Airtable Python Client#

Overview#

一些 Developer 需要知道的点:

  • Authentication: Airflow 使用 personal access token 进行认证, 每个 token 都有对应的 scope 和权限.

  • Rate Limits: 每秒钟最多 50 个 request.

  • List records: 每个 page 最多 100 条记录.

Example#

 1# -*- coding: utf-8 -*-
 2
 3import typing as T
 4from pathlib import Path
 5from diskcache import Cache
 6from pyairtable import Api
 7from rich import print as rprint
 8
 9cache = Cache(str(Path.home().joinpath(".projects", "pyairtable", ".cache")))
10
11path_pac = Path.home().joinpath(".projects", "pyairtable", "pac")
12TOKEN = path_pac.read_text().strip()
13api = Api(TOKEN)
14
15base_name = "Prompt Engineer"
16_key = "Prompt Engineer base id"
17prompt_engineer_base_id = cache.get(_key)
18if prompt_engineer_base_id is None:
19    res = api.bases()
20    for base in res:
21        if base.name == base_name:
22            prompt_engineer_base_id = base.id
23            cache.set(_key, prompt_engineer_base_id)
24            break
25    if prompt_engineer_base_id is None:
26        raise ValueError(f"Can not find base with name {base_name}")
27
28prompt_engineer_base = api.base(base_id=prompt_engineer_base_id)
29_key = "prompt engineer base tables"
30table_mapper: T.Dict[str, str] = cache.get(_key)
31if table_mapper is None:
32    table_mapper = {}
33    for table in prompt_engineer_base.tables():
34        table_mapper[table.name] = table.id
35    cache.set(_key, table_mapper)
36
37tb = api.table(prompt_engineer_base_id, table_mapper["Prompt"])
38for record in tb.all():
39    print(record)