| title | Airtable |
|---|---|
| noIndex | false |
| noContent | false |
Deepnote can query data from Airtable bases. Follow these docs to create your own notebook to connect to Airtable, query data, and visualize it.
Follow these instructions to find your token.
Change the variable AIRTABLE_TOKEN to match your token. If you'd like to keep your data secure, consider storing the token as an environment variable. Environment variables in Deepnote are encrypted and provide a secure way of storing sensitive data.
Find the ID of the Airtable base you want to fetch data from by following these instructions.
Change the variable AIRTABLE_BASE_ID to match the base ID you just found.
Change the variable AIRTABLE_TABLE_NAME to match the name of the table you want to fetch data from. This is the title of the tab in the Airtable UI.
AIRTABLE_TOKEN = 'your-airtable-token'
AIRTABLE_BASE_ID = 'you-base-id'
AIRTABLE_TABLE_NAME = 'your-table-name'For more security in your notebook, we strongly recommend to use environment variables
Since there was a change in Airtable API, the old way of using pyairtable will not work. But don't worry we got you covered. Use requests to fetch data from API.
We will request the API with our base ID, table name, and token.
url = f'https://api.airtable.com/v0/{AIRTABLE_BASE_ID}/{AIRTABLE_TABLE_NAME}'
headers = {
'Authorization': f'Bearer {AIRTABLE_TOKEN}',
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
records = response.json().get('records', [])
data = [record['fields'] for record in records]
df = pd.DataFrame(data)
print(df)One more function to fetch Airtable table and we are good to go.
def fetch_airtable_data(base_id, table_name, access_token):
url = f'https://api.airtable.com/v0/{base_id}/{table_name}'
headers = {'Authorization': f'Bearer {access_token}'}
all_records = []
offset = None
while True:
params = {'offset': offset} if offset else {}
response = requests.get(url, headers=headers, params=params)
if response.status_code != 200:
print(f"Failed to fetch data: {response.status_code}")
break
records = response.json().get('records', [])
all_records.extend(records)
offset = response.json().get('offset')
if not offset:
break
data = [record['fields'] for record in all_records]
return pd.DataFrame(data)The notebook will set up a table object that you can use to fetch any data from your table. For example, the code below fetches the data and converts it to a Pandas DataFrame.
df = fetch_airtable_data(AIRTABLE_BASE_ID, AIRTABLE_TABLE_NAME, AIRTABLE_TOKEN)
dfYou can run queries against our DataFrame using Deepnote's built-in SQL. You can learn more about SQL blocks on our docs.
SELECT
"Estimated Value",
"Priority",
"Status"
FROM
df
WHERE
NOT ("Status" = 'Closed' OR "Status" = 'Lost')Deepnote can visualize data frames out of the box. You can learn more about chart blocks on our docs. If you want to do something more sophisticated, you can use visualization libraries like Altair or Plotly.
If you don't want to do it yourself, here is template notebook
Click the Duplicate button on the top right corner to do this. Once you've got the template in your Deepnote workspace, you can connect it to your Airtable base, don't forget to replace your own token and base id.
Now that you're querying data, you can share it with your team. You can even turn your charts into a shareable dashboard.