Skip to main content

Python 3 tools for interacting with Notion API

Project description

NotionDB

Python 3 tools for interacting with Notion API:

  • API client

  • Relational database wrapper

Changelog

  • 11/11: Add column list, column block

Installation

pip install notiondb

API client

from notiondb import NotionApi

api = NotionApi(API_TOKEN)  # Token from Internal Integration

# Databases
databases, next_cursor = api.get_databases()

api.create_database(parent_id, title, properties)

api.get_database(id)

api.update_database(id, title, properties)

pages, next_cursor = api.query_database(filter, sorts, start_cursor, page_size)

# Pages
pages, next_cursor = api.get_pages(query, start_cursor, page_size)

# Create page in database
api.create_page('database_id', parent_id, title, properties, children, icon, cover)
# Create page in parent page
api.create_page('page_id', parent_id, title, properties, children, icon, cover)

api.get_page(id)

api.update_page(id, properties, archived)

# Get page's block children
blocks, next_cursor = api.get_block_children(id, start_cursor, page_size)

api.append_block_children(id, children)

Wrapper for relational database

Interacting with Notion database as a relational database.

Create database

from notiondb import NotionDatabase
from notiondb.fields import *

properties = [
    TitleField(name='Name'),
    RichTextField(name='Description'),
    CheckboxField(name='In stock'),
    SelectField(name='Food group', options=[
        {
            "name": "🥦Vegetable",
            "color": "green"
        },
        {
            "name": "🍎Fruit",
            "color": "red"
        },
        {
            "name": "💪Protein",
            "color": "yellow"
        }
    ]),
    NumberField(name='Price', format='dollar'),
]

# Create new database
database = NotionDatabase(TOKEN, parent_id=NOTION_PARENT_PAGE_ID, title='Database title', properties=properties)

# Or get existing database
database = NotionDatabase(TOKEN, database_id=DATABASE_ID)

Define document's structure

from notiondb import NotionModel
from notiondb.fields import *


class TestModel(NotionModel):
  
    def __init__(self, database=None, id=None):
        super().__init__(database=database, id=id)

        self.name = TitleField('Name')
        self.description = RichTextField('Description')
        self.in_stock = CheckboxField('In stock')
        self.food_group = SelectField('Food group')
        self.price = NumberField('Price')

Add a row

model = TestModel(database)

model.name.value = 'Name'
model.description.value = 'Description'
model.in_stock.value = True
model.food_group.value = '🥦Vegetable'
model.price.value = 2.33

model.save()

Update a row

# Get a row from id
model = TestModel.from_id(database, row_id)
# Or from data retrieved by API
model = TestModel.from_data(database, data)

model.name.value = 'Name updated'

model.save()

Append block children to page

from notiondb.block import *

children = [
    TableOfContentsBlock(),
    ParagraphBlock('paragraph 1'),
    ParagraphBlock('paragraph 2', link='https://example.com'),
]
model.append_children(children)

Query database

Filter & sorts reference

cursor = TestModel.objects(database).get(filter=filter, sorts=sorts, limit=limit)
for item in cursor:
    # do something

Get row's data

# Get row's block children
children = model.get_children()

# Parse to JSON
data = model.to_json(includes_children=True)

Delete a row

model.delete()

Testing

Create .env file in ./tests

NOTION_TOKEN=
# Parent page id for testing
DEV_PAGE_NOTION_ID=

Run tests

python -m unittest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notiondb-1.0.2.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notiondb-1.0.2-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file notiondb-1.0.2.tar.gz.

File metadata

  • Download URL: notiondb-1.0.2.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.9

File hashes

Hashes for notiondb-1.0.2.tar.gz
Algorithm Hash digest
SHA256 55d42ae8b95343e68ed29be3abeaff812590237f6fbd9d9f38c1093e2115db9e
MD5 d884e7371cdc6b185b976880ee6d6927
BLAKE2b-256 6b126de793137cdcde2f539d6e04e12d9c9072fc5e60958a7e8fdd8fa9b7b018

See more details on using hashes here.

File details

Details for the file notiondb-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: notiondb-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.9

File hashes

Hashes for notiondb-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b8d06ac04d92e899bfcb5d009258c98f8ca6b6bca24fdf1c6cb2f0311c78ea8d
MD5 aec355e48069768aad9e50ee43240713
BLAKE2b-256 74d61c2460cf523f5293da1fca767401efaf67dbe368b248522aa9c1a90031f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page