Skip to main content

Client-agnostic model wrapper for Notion API

Project description

basic-notion

Client-agnostic model wrapper for Notion API.

This library does not do any interaction over the network itself, so it can be used with any existing client that exposes as output and accepts as input raw JSONable data.

Note that this project is at its infancy stage, so major changes in its structure and API are quite possible and even probable in the near future.

Installation

Just like any other python package out there, it can be installed via pip:

pip install basic-notion

Basic Examples

Describing Models

from basic_notion.page import NotionPage, NotionPageList
from basic_notion.field import SelectField, TitleField, MultiSelectField


class ReadingListItem(NotionPage):
    type: SelectField = SelectField(property_name='Type')
    name: TitleField = TitleField(property_name='Name')
    status: SelectField = SelectField(property_name='Status')
    authors: MultiSelectField = MultiSelectField(property_name='Author')


class ReadingList(NotionPageList[ReadingListItem]):
    ITEM_CLS = ReadingListItem

Using With notion-sdk-py

Fetching a list of pages from a database

import asyncio
import os

from notion_client import AsyncClient
from basic_notion.query import Query

from models import ReadingList


async def get_reading_list() -> ReadingList:
    database_id = os.environ['DATABASE_ID']
    notion_token = os.environ['NOTION_TOKEN']
    notion = AsyncClient(auth=notion_token)
    data = await notion.databases.query(
        **Query.database(
            database_id
        ).filter(
            # Construct filter using model's field
            # (only one filter expression is supported)
            ReadingList.item.type.filter.equals('Book')
        ).sorts(
            # And, similarly, the result's sorting
            # (multiple fields can be listed here)
            ReadingList.item.name.sort.ascending
        ).serialize()
    )
    return ReadingList(data=data)


def print_reading_list(reading_list: ReadingList) -> None:
    for item in reading_list.items():
        print(f'[{item.type.name}] {item.name.one_item.content}')


async def main() -> None:
    reading_list = await get_reading_list()
    print_reading_list(reading_list)


asyncio.run(main())

(assuming you put the previous code in models.py)

Creating a new page

from notion_client import Client
from models import ReadingListItem

def create_page(client: Client, database_id: str) -> ReadingListItem:
    page_data = ReadingListItem.generate(
        parent={'database_id': database_id},
        type='Book',
        name=['The Best Book Ever'],
        authors=['John Doe'],
    )
    response = client.pages.create(**page_data)
    item = ReadingListItem(data=response)
    # assert len(item.id) == 36
    # assert item.type.name == 'Book'
    # assert item.name.get_text() == 'The Best Book Ever'
    # assert item.authors.get_text() == 'John Doe'
    return item

See notion-sdk-py's homepage for more info on the client: https://github.com/ramnes/notion-sdk-py

You can also see the files in tests/ for more examples and more thorough usage of attributes and properties

Development and Testing

Configuring the test environment

Install

pip install -Ue .[testing]

Create file .env with the following content:

NOTION_API_TOKEN=<your-notion-token>
DATABASE_ID=<your-database-id>
ROOT_PAGE_ID=<your-page-id>

Where:

  • <your-notion-token> is your Notion API developer's token (you will need to create a Notion integration for this: https://www.notion.so/my-integrations);
  • <your-database-id> is a database that you can use for test purposes (with read/write access and a schema corresponding to the model structure from tests/models.py - Notion's default Reading List will do);
  • <your-page-id> is the ID of a page where the tests will create new pages.

Testing

Run the tests:

pytest tests

And always validate typing:

mypy src/basic_notion

Or simply

make test

(it will run all test commands)

Links

Homepage on GitHub: https://github.com/altvod/basic-notion

Project's page on PyPi: https://pypi.org/project/basic-notion/

Notion API: https://developers.notion.com/

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

basic-notion-0.2.4.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

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

basic_notion-0.2.4-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file basic-notion-0.2.4.tar.gz.

File metadata

  • Download URL: basic-notion-0.2.4.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for basic-notion-0.2.4.tar.gz
Algorithm Hash digest
SHA256 df5f5329e0f6b2ca25bd75980fc7c65e4e26460e977537afd73e6a2d2d04acd7
MD5 5dd757ca86cb972187169476de816992
BLAKE2b-256 f0c888543c27ca4b9883273bd9431bee15b85ba08b83b3d1ce59f3bc784a9e1b

See more details on using hashes here.

File details

Details for the file basic_notion-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: basic_notion-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for basic_notion-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2953352ff6ffb4f927731fe18827fa11718ddfdf1987699dde96b419a3ff590a
MD5 2fe0df297037d24eaae740673ea26263
BLAKE2b-256 6974100b18b1b427ac07448f35262f5d3f2311d8dfc754dc3f0a6addd5624f32

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