Skip to main content

notion-objects

Build Status PyPI Version PyPI License Codestyle

A Python library that makes it easy to work with notion databases, built on top of notion-sdk-py. It provides a higher-level API with a data mapper, allowing you to define custom mappings between notion database records and your Python objects.

With notion-objects you can:

User guide

Defining models

Suppose your database tasks has four fields, the title Task, a date range Date, and a person Assigned to, and a status field Status. You want to transform notion database queries into records of:

{
  "task": "my task",
  "date_start": "2022-01-01",
  "date_end": "2022-01-02",
  "assigned_to": "Thomas",
  "status": "In progress"
}

First, declare a model that contains all the necessary transformations as descriptors:

from notion_objects import *

class Task(NotionObject):
    task = TitleText("Task")
    assigned_to = Person("Assigned to")
    date_start = DateRangeStart("Date")
    date_end = DateRangeEnd("Date")
    closed_at = Date("Closed at")
    status = Status("Status")

Now, when you have queried a database, you can instantiate Task objects with the results of the API call:

response = requests.post("https://api.notion.com/v1/databases/{database_id}/query", ...)

for item in response.json()['results']:
    t = Task(item)
    print(t.task)  # access attribute values
    print(t.to_json())  # prints the record in the json format show earlier

Querying Databases

notion-objects adds data-mapping around notion-sdk-py. The Database class is uses a type parameter to map notion objects to the data models you defined.

Here's a code snippet showing how to iterate over all pages in a databases that were updated after 2022-10-08, using our built-in Page model that holds the root page attributes.

from notion_client import Client
from notion_objects import Database, Page

notion = Client(auth=os.environ['NOTION_TOKEN'])

database: Database[Page] = Database(Page, database_id="123456789abcdef1234567890abcdef1", client=notion)

result = database.query({
    "filter": {
        "timestamp": "last_edited_time",
        "last_edited_time": {
            "after": "2022-10-08"
        }
    }
})
for page in result:
    print(page.id, page.created_time, page.last_edited_time)

You could also use DynamicNotionObject if you're too lazy to create a model for your database. notion-objects will map the data types in a best-effort way. You can also iterate directly over the database to fetch all records:

from notion_objects import Database, DynamicNotionObject

database = Database(DynamicNotionObject, ...)

for record in database:
    print(record.to_json())  # will print your database record as JSON

NOTE not all types have yet been implemented. Type mapping is very rudimentary.

Updating records

You can update database records by simply calling attributes with normal python assignments. The data mapper will map the types correctly to Notion's internal format. You can then call Database.update(...) to run an update API call. notion-objects keeps track of all the changes that were made to the object, and only sends the changes.

database: Database[Task] = Database(Task, ...)

task = database.find_by_id("...")
task.status = "Done"
task.closed_at = datetime.utcnow()
database.update(task)

Note not all properties can be set yet.

Creating records

Similarly, you can also create new pages. You can use NotionObject.new() on any subclass to create new unmanaged instances of that type. Then, call Database.create(...) to create a new item in the database.

database: Database[Task] = Database(Task, ...)

task = Task.new()
task.task = "My New Task"
task.status = "In progress"
task.assigned_to = "6aa4d3cd-3928-4f61-9072-f74a3ebfc3ca"

task = database.create(task)
print(task.id)  # will print the page ID that was created

Release files for notion-objects 0.6.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for notion-objects 0.6.5
File Size Uploaded
notion_objects-0.6.5.tar.gz 18.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for notion-objects 0.6.5
File Interpreter ABI Platform
notion_objects-0.6.5-py3-none-any.whl Python 3 none any Details

Total release size: 37.6 kB

Release files / notion_objects-0.6.5.tar.gz

Download URL notion_objects-0.6.5.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6d88f660e49a1a4f989a28b52931945fac2e284b2ebdd8edeeb72e5e30af82f9
BLAKE2b-256 checksum
How to use checksums
90513d79f4653e11c4a60475088cbf7f42ff1aa144ff9e2c4fea7d82372d9d3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release files / notion_objects-0.6.5-py3-none-any.whl

Download URL notion_objects-0.6.5-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b883eddbad5b7ba7ad11743313330a31161790a9e447ad26d6f62cf3437b724e
BLAKE2b-256 checksum
How to use checksums
8f1438e72bfcf3d36bdaa504b164a9405de8275371893e84215580e64b221ea7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.5

Release history Release notifications | RSS feed

This release

0.6.5 This release

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page