database
notion.database
API for exchanging information with Notion database. Main features include, 1) creating a notion database with property assignments. 2) adding dictionary data to existing notion database.
RESEARCH_API_PATH
module-attribute
BasicProperty
Notion database basic properties are implemented. It converts values into property object structures specified by the notion api. Additionally, it can check whether given value is valid for the property. Note that status property is not implemented due to the limited notion api capabilites.
Source code in ResearchToolkit/notion/property.py
__init_subclass__
Source code in ResearchToolkit/notion/property.py
__call__
get_value
classmethod
extracts values from a property object structure specified by the notion api. (opposite operation of call method)
Parameters:
-
property_(dict) –property dictionary output of notion api. must include "type" key.
Returns:
-
_type_–value of given property. ex) 3.14 in above example.
Source code in ResearchToolkit/notion/property.py
get_filter
classmethod
Source code in ResearchToolkit/notion/property.py
Title
use_saige_token_as_default
Source code in ResearchToolkit/notion/common.py
get_token
convert_data_into_property
convert_data_into_property(data: Dict[str, Any], fields: FieldType)
converts dictionary data into a property form required by notion api. key of data and fields shoud match!
e.g) data = {"exp_name": "cls_base", "accuracy": 0.99} fields = {"exp_name": "title", "accuracy": "number"}
return:
{
"exp_name": {
"title": [
"text": {"content": "cls_base"}
]
},
"accuracy": {
"number": 0.99
}
}
Parameters:
-
data(Dict[str, Any]) –key: property name, value: property value
-
fields(FieldType) –key: property name, value: property type
Returns:
-
_type_–property required by notion api
Source code in ResearchToolkit/notion/database.py
make_database_property_schema
make_database_property_schema(fields: FieldType, relations: Dict[str, Dict[str, str]] = None) -> Dict
Parameters:
-
fields(FieldType) –key: name of the property, value: property type (should be one of the keys in
PROPERTIES) e.g) {"exp_name": "title", "accuracy": "number"} create a database whose property names are exp_name, accuracy and their property types are title and number. -
relations(Dict[str, Dict[str, str]], default:None) –key: name of the relation property, value: dictionary with "database_id" and "type" info. "database_id" is notion database id to relate to "type" should be one of "single_property" or "dual_property". e.g) {"related_task": {"database_id": (database_id), "type": "dual_property}} !NOTE: relation database should also be shared with notion integration! Defaults to None.
Returns:
-
Dict(Dict) –Notion API database property schema (https://developers.notion.com/reference/property-schema-object)
Source code in ResearchToolkit/notion/database.py
_add_to_database
_add_to_database(database_id: str, properties: dict, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None)
add a page with properties to the notion database.
Parameters:
-
database_id(str) –notion database id to add page.
-
properties(dict) –page property dictionary in specific format described in notion api docs. see (https://developers.notion.com/reference/property-value-object). basic format: { "(property_name)":{ "(property_type)": (value whose type and structure are specific to property_type) } } This can be easily obtained by using child classes of
BasicProperty. refer 'convert_data_into_property'. -
token_path(str, default:RESEARCH_API_PATH) –description. Defaults to RESEARCH_API_PATH.
-
token(Optional[str], default:None) –description. Defaults to None.
Returns:
-
_type_–request response code (200 if succeeded) and response text, page_id (None if request failed)
Source code in ResearchToolkit/notion/database.py
create_database
create_database(page_id: str, fields: FieldType, relations: Dict[str, Dict[str, str]] = {}, title: Optional[str] = None, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None)
creates a notion database in a page_id page.
it is recommended to add "user" field in fields which specifies the user of api when collaborating on a database.
Parameters:
-
page_id(str) –notion page id
-
fields(FieldType) –key: name of the property, value: property type (should be one of the keys in
PROPERTIES) e.g) {"exp_name": "title", "accuracy": "number"} create a database whose property names are exp_name, accuracy and their property types are title and number. -
relations(Dict[str, Dict[str, str]], default:{}) –key: name of the relation property, value: dictionary with "database_id" and "type" info. "database_id" is notion database id to relate to "type" should be one of "single_property" or "dual_property". e.g) {"related_task": {"database_id": (database_id), "type": "dual_property}} !NOTE: relation database should also be shared with notion integration!
-
title(str, default:None) –title of database. Defaults to None ("untitled" database will be created).
-
token_path(str, default:RESEARCH_API_PATH) –description. Defaults to RESEARCH_API_PATH.
-
token(Optional[str], default:None) –description. Defaults to None.
Returns:
-
_type_–request response code (200 if succeeded), response text, database_id (None if request failed)
Source code in ResearchToolkit/notion/database.py
retrieve_database_fields
retrieve_database_fields(database_id: str, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None, is_page: bool = False)
retrieves database fields
Parameters:
-
database_id(_type_) –notion database id
-
token_path(_type_, default:RESEARCH_API_PATH) –description. Defaults to RESEARCH_API_PATH.
-
token(_type_, default:None) –description. Defaults to None.
-
is_page(bool, default:False) –True if the id is for a page. Defaults to None.
Returns:
-
_type_–fields dictionary - key: property name, value: property type e.g) {"exp_name": "title", "accuracy": "number"}
Source code in ResearchToolkit/notion/database.py
add_data_to_database
add_data_to_database(database_id: str, data: dict, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None, user: Optional[Dict[str, BasicProperty]] = None)
add data to notion database. properties of data values are assigned to match the properties of existing database.
when collaborating on a database, it is recommended to use user option to specfiy who edited the database.
Parameters:
-
database_id(str) –description
-
data(dict) –data to store in database. keys should match names of the database properties
-
token_path(str, default:RESEARCH_API_PATH) –description. Defaults to RESEARCH_API_PATH.
-
token(Optional[str], default:None) –description. Defaults to None.
-
user(Optional[Dict[str, BasicProperty]], default:None) –use when collaborating on a databaese to specify editor of the database. key should be the property name of the database responsible for tracking api user. if vaule is `Person`, a notification will be send in notion.
Returns:
-
_type_–request response code (200 if succeeded) and response text, page_id (None if request failed)
Source code in ResearchToolkit/notion/database.py
query_database
query_database(database_id: str, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None, request_body: Optional[dict] = None)
query a database.
refer https://developers.notion.com/reference/post-database-query
Parameters:
-
database_id(str) –description
-
token_path(str, default:RESEARCH_API_PATH) –description. Defaults to RESEARCH_API_PATH.
-
token(Optional[str], default:None) –description. Defaults to None.
-
request_body(Optional[dict], default:None) –request body for query with filtering or sorting. Defaults to None. Check https://developers.notion.com/reference/post-database-query for detailed usage of BODY_PARAM.
Returns:
-
_type_–request response code (200 if succeeded), response text, results (None if request failed)
Source code in ResearchToolkit/notion/database.py
get_property_value
Source code in ResearchToolkit/notion/database.py
get_database_in_pd_dataframe
get_database_in_pd_dataframe(database_id: str, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None, request_body: Optional[dict] = None)
get notion database and return it in pandas dataframe.
Parameters:
-
database_id(str) –description
-
token_path(str, default:RESEARCH_API_PATH) –description. Defaults to RESEARCH_API_PATH.
-
token(Optional[str], default:None) –description. Defaults to None.
-
request_body(Optional[dict], default:None) –request body for query with filtering or sorting. Defaults to None. Check https://developers.notion.com/reference/post-database-query for detailed usage of BODY_PARAM.
Returns:
-
–
pd.DataFrame: description
Source code in ResearchToolkit/notion/database.py
update_page_data
Database page의 data를 업데이트 합니다.
Source code in ResearchToolkit/notion/database.py
update_database_fields
update_database_fields(token: str, database_id: str, fields: FieldType)
Database fields (properties) 를 업데이트 합니다.
Source code in ResearchToolkit/notion/database.py
update_database_fields_safe
update_database_fields_safe(token: str, database_id: str, fields: FieldType)
Database fields (properties) 를 업데이트 합니다. 데이터베이스에 동일한 이름으로 다른 타입의 프로퍼티가 존재하면 에러를 레이즈 합니다.