notion
Module diagram
classDiagram
class notion {
}
class block {
}
class common {
}
class database {
}
class filter {
}
class property {
}
class user {
}
notion --> block
notion --> database
notion --> user
block --> common
database --> common
database --> property
property --> common
property --> filter
property --> user
user --> common
notion
retrieve_block_children
retrieve_block_children(page_id: str, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None)
get all blocks on notion page. Args: page_id (str): notion page_id to search token_path (str, optional): description. Defaults to RESEARCH_API_PATH. token (Optional[str], optional): description. Defaults to None.
Returns:
-
_type_–request response code (200 if succeeded) and response text, block_children
Source code in ResearchToolkit/notion/block.py
find_database_on_page
find all database on page.
Returns:
-
Dict[str, str]–Dict[str, str]: dictionary whose keys are database ids and values are database names
Source code in ResearchToolkit/notion/block.py
append_block_children
append_block_children(page_id: str, block: Union[dict, List[dict]], token_path: str = RESEARCH_API_PATH, token: Optional[str] = None)
append block or blocks to a page
Parameters:
-
page_id(str) –description
-
block(Union[dict, List[dict]]) –output or list of outputs of
Blockobjects e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()] -
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
Source code in ResearchToolkit/notion/block.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
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
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
get_notion_users
Source code in ResearchToolkit/notion/user.py
block
Block
Notion Block objects. It converts values into block object structures specified by the notion api. Additionally, it can check whether given value is valid for the property.
Source code in ResearchToolkit/notion/block.py
__init_subclass__
Heading1
Bases: Block
Source code in ResearchToolkit/notion/block.py
Heading2
Bases: Heading1
Source code in ResearchToolkit/notion/block.py
Heading3
Bases: Heading1
Source code in ResearchToolkit/notion/block.py
Paragraph
Bases: Heading1
summary
Parameters:
-
value(_type_) –description
-
color(Optional[str], default:None) –description. Defaults to None.
-
children(Optional[Union[dict, List[dict]]], default:None) –children should be output or list of
Blockobject output. e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()]
Source code in ResearchToolkit/notion/block.py
Callout
Bases: Paragraph
summary
Parameters:
-
value(_type_) –description
-
color(Optional[str], default:None) –description. Defaults to None.
-
children(Optional[Union[dict, List[dict]]], default:None) –children should be output or list of
Blockobject output. e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()]
Source code in ResearchToolkit/notion/block.py
Quote
Bases: Paragraph
summary
Parameters:
-
value(_type_) –description
-
color(Optional[str], default:None) –description. Defaults to None.
-
children(Optional[Union[dict, List[dict]]], default:None) –children should be output or list of
Blockobject output. e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()]
Source code in ResearchToolkit/notion/block.py
BulletedListItem
BulletedListItem(value, color: Optional[str] = None, children: Optional[Union[dict, List[dict]]] = None)
Bases: Paragraph
summary
Parameters:
-
value(_type_) –description
-
color(Optional[str], default:None) –description. Defaults to None.
-
children(Optional[Union[dict, List[dict]]], default:None) –children should be output or list of
Blockobject output. e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()]
Source code in ResearchToolkit/notion/block.py
NumberedListItem
NumberedListItem(value, color: Optional[str] = None, children: Optional[Union[dict, List[dict]]] = None)
Bases: Paragraph
summary
Parameters:
-
value(_type_) –description
-
color(Optional[str], default:None) –description. Defaults to None.
-
children(Optional[Union[dict, List[dict]]], default:None) –children should be output or list of
Blockobject output. e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()]
Source code in ResearchToolkit/notion/block.py
ToDo
ToDo(value, checked: Optional[bool] = None, color: Optional[str] = None, children: Optional[Union[dict, List[dict]]] = None)
Toggle
Bases: Paragraph
summary
Parameters:
-
value(_type_) –description
-
color(Optional[str], default:None) –description. Defaults to None.
-
children(Optional[Union[dict, List[dict]]], default:None) –children should be output or list of
Blockobject output. e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()]
Source code in ResearchToolkit/notion/block.py
Code
Bases: Block
Source code in ResearchToolkit/notion/block.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
register
make_rich_text_blocks
text를 block 길이 제한 만큼 잘라서 block들의 리스트로 변환합니다.
Parameters:
-
name(str) –block 종류
-
text(str) –컨텐츠 텍스트
Returns:
-
List[Dict]–List[Dict]: append_block_children에 입력할 수 있는 block들
Source code in ResearchToolkit/notion/block.py
retrieve_block_children
retrieve_block_children(page_id: str, token_path: str = RESEARCH_API_PATH, token: Optional[str] = None)
get all blocks on notion page. Args: page_id (str): notion page_id to search token_path (str, optional): description. Defaults to RESEARCH_API_PATH. token (Optional[str], optional): description. Defaults to None.
Returns:
-
_type_–request response code (200 if succeeded) and response text, block_children
Source code in ResearchToolkit/notion/block.py
find_database_on_page
find all database on page.
Returns:
-
Dict[str, str]–Dict[str, str]: dictionary whose keys are database ids and values are database names
Source code in ResearchToolkit/notion/block.py
append_block_children
append_block_children(page_id: str, block: Union[dict, List[dict]], token_path: str = RESEARCH_API_PATH, token: Optional[str] = None)
append block or blocks to a page
Parameters:
-
page_id(str) –description
-
block(Union[dict, List[dict]]) –output or list of outputs of
Blockobjects e.g) Paragraph("hello world")() or [Heading1("Tutorial")(), Paragraph("hello world")()] -
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
Source code in ResearchToolkit/notion/block.py
common
RESEARCH_API_PATH
module-attribute
pascal_to_snake
get_token
use_saige_token_as_default
Source code in ResearchToolkit/notion/common.py
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.
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) 를 업데이트 합니다. 데이터베이스에 동일한 이름으로 다른 타입의 프로퍼티가 존재하면 에러를 레이즈 합니다.
Source code in ResearchToolkit/notion/database.py
filter
Filter
Filter class for handling Notion Filter object. Please see https://developers.notion.com/reference/post-database-query-filter for detail.
You can now query to notion DB with filtering as following:
import SaigeToolkit.SaigeToolkit.notion as notion
filter_1 = notion.property.Select.get_filter("select_col", "equals", "sel1")
filter_2 = notion.property.Number.get_filter("number_col", "does_not_equal", 10)
filter_3 = notion.property.Checkbox.get_filter("checkbox_col", "does_not_equal", True)
filter_4 = notion.filter.Filter.compound("and", [filter_1, filter_2, filter_3])
_, _, results = notion.database.query_database(
db_id,
RESEARCH_API_PATH,
request_body=filter_4.to_dict()
)
See notion.so/8cd58f07a254405ebb1bc327d690c8aa for more examples.
Source code in ResearchToolkit/notion/filter.py
to_dict
Source code in ResearchToolkit/notion/filter.py
get_avaliable_filter_ops
Source code in ResearchToolkit/notion/filter.py
property
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
Text
Title
Number
Bases: BasicProperty
Source code in ResearchToolkit/notion/property.py
Select
MultiSelect
Bases: BasicProperty
value should be a string or list of strings.
Source code in ResearchToolkit/notion/property.py
__bool__
__call__
Date
Bases: BasicProperty
date should be in iso-format.
Source code in ResearchToolkit/notion/property.py
__bool__
Source code in ResearchToolkit/notion/property.py
__call__
Source code in ResearchToolkit/notion/property.py
Person
Bases: BasicProperty
value should be either name or email used in Notion.
Source code in ResearchToolkit/notion/property.py
user_names
class-attribute
instance-attribute
user_emails
class-attribute
instance-attribute
validate_member
get_id
__bool__
__call__
get_person_with_user_info
classmethod
Files
Bases: BasicProperty
only files on remotes can be uploaded.
Source code in ResearchToolkit/notion/property.py
__bool__
__call__
Checkbox
Bases: BasicProperty
Source code in ResearchToolkit/notion/property.py
Url
Bases: BasicProperty
Source code in ResearchToolkit/notion/property.py
Email
Bases: BasicProperty
Source code in ResearchToolkit/notion/property.py
Phone
Bases: BasicProperty
Source code in ResearchToolkit/notion/property.py
Relation
Bases: BasicProperty
value should be a notion page id
Source code in ResearchToolkit/notion/property.py
__bool__
Source code in ResearchToolkit/notion/property.py
__call__
user
MEMBER_INFO_PATH
module-attribute
get_notion_users
Source code in ResearchToolkit/notion/user.py
get_git_user
get git user information (name and email) useful when collaborators share a database. can automatically specfiy editor of the database using git user info
Parameters:
-
repo_path(_type_) –git repo path
Returns:
-
_type_–description
Source code in ResearchToolkit/notion/user.py
find_git
find directory with .git file in parent directories. return None if not found.
Parameters:
-
path(_type_) –description
Returns:
-
_type_–description