Skip to content

notion_utils

Some utility functions for working with Notion properties etc.

collapse_rich_text_property(property)

Collapse a Notion Rich Text property into a single string.

Parameters:

Name Type Description Default
property list[dict[str, Any]]

An array of rich text objects (as returned by a rich text property)

required

Returns:

Type Description
str

The plain_text of the rich text objects concatenated.

Source code in ncal/notion_utils.py
 7
 8
 9
10
11
12
13
14
15
def collapse_rich_text_property(property: list[dict[str, Any]]) -> str:
    """Collapse a Notion Rich Text property into a single string.

    Args:
        property: An array of rich text objects (as returned by a rich text property)
    Returns:
        The plain_text of the rich text objects concatenated.
    """
    return "".join((i["plain_text"] for i in property))

get_property_text(notion, notion_page, property_name, property_type)

Get the text contained within several different types of property.

Note: Currently only relation and select are implemented.

Source code in ncal/notion_utils.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def get_property_text(
    notion: notion_client.Client,
    notion_page: dict[str, Any],
    property_name: str,
    property_type: Literal["relation", "select"],
) -> str:
    """Get the text contained within several different types of property.

    Note: Currently only relation and select are implemented.
    """
    text: str
    if property_type == "select":
        text = notion_page["properties"][property_name]["select"]["name"]
    elif property_type == "relation":
        text = get_relation_title(
            notion=notion, notion_page=notion_page, relation_name=property_name
        )
    else:
        raise ValueError
    return text

get_relation_title(notion, notion_page, relation_name)

Get the title of the first page in a relation property.

Source code in ncal/notion_utils.py
18
19
20
21
22
23
24
25
26
27
28
29
30
def get_relation_title(
    notion: notion_client.Client, notion_page: dict[str, Any], relation_name: str
) -> str:
    """Get the title of the first page in a relation property."""
    relation_property: list = notion_page["properties"][relation_name]["relation"]
    if relation_property:
        relation_id: str = relation_property[0]["id"]
        relation_title: dict = notion.pages.properties.retrieve(
            relation_id, "title"
        )  # type:ignore
        return relation_title["results"][0]["title"]["plain_text"]
    else:
        return ""
Back to top