Skip to content

Instantly share code, notes, and snippets.

@lgaud
Created October 11, 2022 13:38
Show Gist options
  • Save lgaud/98283104433d12264f6fc32b19074084 to your computer and use it in GitHub Desktop.
Save lgaud/98283104433d12264f6fc32b19074084 to your computer and use it in GitHub Desktop.
Creates 2 Notion Databases, where the second has a Relation and Roll Ups to the first.
import json
import os
from dotenv import load_dotenv
import requests
class NotionClient():
def __init__(self, notion_key):
self.notion_key = notion_key
self.default_headers = {'Authorization': f"Bearer {self.notion_key}",
'Content-Type': 'application/json', 'Notion-Version': '2022-06-28'}
self.session = requests.Session()
self.session.headers.update(self.default_headers)
def create_database(self, data):
url = "https://api.notion.com/v1/databases"
response = self.session.post(url, json=data)
return response.json()
def main(page_id):
notion_client = NotionClient(os.getenv('NOTION_KEY'))
print(f"*** Creating database under {page_id} ***")
minimum = {
"parent": {
"type": "page_id",
"page_id": page_id
},
"properties": {
"Name": {
"title": {}
}
}
}
basic_create_response = notion_client.create_database(minimum)
print(json.dumps(basic_create_response, indent=2))
catches = {
"parent": {
"type": "page_id",
"page_id": page_id
},
"icon": {
"type": "emoji",
"emoji": "🐟"
},
"title": [
{
"type": "text",
"text": {
"content": "Catches",
"link": None
}
}
],
"properties": {
"Name": {
"title": {}
},
"Weight (lbs)": {
"number": {}
},
"Location": {
"rich_text": {}
},
"Date": {
"date": {}
},
"Species": {
"select": {
"options": [
{
"name": "Northern Pike",
"color": "green"
},
{
"name": "Walleye",
"color": "red"
},
{
"name": "Smallmouth Bass",
"color": "pink"
}
]
}
}
}
}
print("*** Creating catches database ***")
catches_create_response = notion_client.create_database(catches)
print(json.dumps(catches_create_response, indent=2))
trips = {
"parent": {
"type": "page_id",
"page_id": page_id
},
"title": [
{
"type": "text",
"text": {
"content": "Trips",
"link": None
}
}
],
"cover": {
"type": "external",
"external": {
"url": "https://images.unsplash.com/photo-1597956959732-9b8cb12f8960?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1332&q=80"
}
},
"properties": {
"Trip Name": {
"title": {}
},
"Catches": {
"relation": {
"database_id": catches_create_response.get("id"),
"type": "dual_property",
"dual_property": {}
}
},
"Total Weight Caught": {
"rollup": {
"rollup_property_name": "Weight (lbs)",
"relation_property_name": "Catches",
"function": "sum"
}
},
"Total Fish Caught": {
"rollup": {
"rollup_property_name": "Name",
"relation_property_name": "Catches",
"function": "count_values"
}
}
}
}
print("*** Creating Trips Database ***")
trips_create_response = notion_client.create_database(trips)
print(json.dumps(trips_create_response, indent=2))
if __name__ == "__main__":
import argparse
load_dotenv(override=True)
parser = argparse.ArgumentParser(description='Create a Notion Database.')
parser.add_argument('page_id', type=str,
help='A Notion Page ID to create the database under')
args = parser.parse_args()
main(args.page_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment