pynodb
Introduction
This is a really simple package for easy use of the Notion database. I developed this package to automate the repetitve in-house tasks using Notion. We use Notion as frontend and also backend(database table). When using pure Notion database API, it gives us different structure for each column's data type. Therefore we have to parse json response for each data type and it is very cumbersome. With this package, you can get simple and important data which are column name and its value.
Install
pip install pynodb
Requirement
Please follow this guides first to use this package. https://developers.notion.com/docs#:~:text=Postman%20workspace-,Getting%20started,-Learn%20how%20to
Usage
If you are ready with Notion configuration, you can start from here. There are two modules.
from pynodb.notion_database_client import NotionDatabaseClient
from pynodb.database_parser import DatabaseParser
Quick Start
Usually, I create my Notion database in Notion first and get its ID to use API.
You can get URL and ID of database when you click Open as page
button from top right of database.
https://www.notion.so/myworkspace/a8aec43384f447ed84390e8e42c2e089?v=...
----------------------------------|--------- Database ID --------|------
Let's use this package quickly. This is sample Notion database
secret_key = "<your-secret-key>"
database_id = "<your-database-id>"
notion_database_client = NotionDatabaseClient(secret_key=secret_key, database_id=database_id)
database = notion_database_client.fetch_database()
print(database)
You can get original result from Notion API when printing database
. This result is quite complex to read and parse
Below is the easy way of parsing your database result.
my_database = DatabaseParser(database)
parsed_database = my_database.parsed_database
print(parsed_database)
Each dictionary in list has 'page_id' and all column names as keys.
You can access each data easily like this.
for page in parsed_database:
page_id = page['page_id']
date_value = page['DATE']['value']
select_value = page['SELECT']['value']
...
Details
NotionDatabaseClient
This module is for using Notion API. You can fetch, update Notion database and create, update Notion pages using this module.
Method | Parameter | Description |
---|---|---|
fetch_database() | limit(=None) , filter(=None) |
Fetch all data from database. You can set limit(number of pages you want) and filter(query condition, please see this link) for this method. |
update_database() | data (json type body) |
Update database. Please see this link for data parameter. |
create_page() | data (json type body) |
Create page. Please see this link for data parameter. |
update_page() | page_id , data (json type body) |
Update page. Please see this link for data parameter. |
get_page() | page_id |
Retrieve a page. Please see this link. |
get_block_children() | block_id |
Retrieve a block. Please see this link. |
append_block_children() | block_id , data (json type body) |
Append block children. Please see this link for data parameter. |
DatabaseParser
There are only private methods in this class. You can only access varaibles.
Variable | Description |
---|---|
raw_database | Raw json response from Notion API |
parsed_database | Parsed json which is for easy use. |
property_names | All property names from database. |
Supported Data Types of DatabaseParser
*This supported data types are only related to DatabaseParser. You can get any data type using NotionDatabaseClient since it gives you raw json response of Notion API.
- TITLE
- RICH_TEXT
- NUMBER
- SELECT
- MULTI_SELECT
- PEOPLE
- DATE
- URL
- CHECKBOX
- RELATION
- ROLLUP (only array type)
- FORMULA
- CREATED_TIME
- LAST_EDITED_TIME
- CREATED_BY
- LAST_EDITED_BY