Notion API master series 16 - Analysis - Update a block

소찬 (Chan)·2022년 10월 24일
0
post-thumbnail

Reference API URL : Update a block

Open the link and check the left menu order. The first order is not 'Add a block' or 'Create a block' and even shows 'Update a block.' Without 'Create a block', use 'Append block children' to create blocks.
Learn how to create a block on the next series, 'Append block children'. And now let's find out how to 'Update a block'.

Call blocks.children.list then you can get block_id information. For instance, if we want to bring the id of the first block, we can write like code as below. Below code details, you can check the last series, 15 - Analysis - 'Retrieve a block & Retrieve block children' out.

page_id = '1f217cc1-225f-4580-8e22-cd7eb8db3c5e'
blocks = notion.blocks.children.list(block_id=page_id)
target_block_id = blocks['results'][0]['id'] # 첫번째 block_id

If we want to put the second block_id, then we can write code as blocks['results'][1]['id']. Now we got block_id so let's see BODY PARAM's {type}.

Refer to this Block object keys page. This page mentioned type.
"paragraph", "heading_1", and so on. We can use one of these objects for what we need. Sure, API rejects and occurs errors even if there are minor wrong forms, so read a page or block first for reference, put a variable from the reading page or block information, change the value, and use it what you want.
Firstly, if we bring block_id into a variable target_block_id, get the information with 'retrieve' with block_id parameter is target_block_id.

value = notion.blocks.retrieve(block_id=target_block_id)

Below is a sample to print with pprint from the above value.

{'archived': False,
 ... ,
 'object': 'block',
 'paragraph': {'color': 'default',
               'rich_text': [{'annotations': {'bold': False,
                                              'code': False,
                                              'color': 'default',
                                              'italic': False,
                                              'strikethrough': False,
                                              'underline': False},
                              'href': None,
                              'plain_text': '페퍼로니가 취향저격',
                              'text': {'content': '페퍼로니가 취향저격', 'link': None},
                              'type': 'text'}]},
... ,
}

Let's get the 'paragraph' part.

paragraph_new = value['paragraph']

Print paragraph_new value, then it shows like below.

{'color': 'default',
               'rich_text': [{'annotations': {'bold': False,
                                              'code': False,
                                              'color': 'default',
                                              'italic': False,
                                              'strikethrough': False,
                                              'underline': False},
                              'href': None,
                              'plain_text': '페퍼로니가 취향저격',
                              'text': {'content': '페퍼로니가 취향저격', 'link': None},
                              'type': 'text'}]}

Just change the 'text' and 'content' values if you need to modify the text.

text_new = '크러스트가 취향저격'
paragraph_new['rich_text'][0]['plain_text'] = text_new
paragraph_new['rich_text'][0]['text']['content'] = text_new

Finally, let's do an update. Change {type} to paragraph, input the paragraph and call API.

notion.blocks.update(block_id=target_block_id, paragraph=paragraph_new)
profile
QA Specialist

0개의 댓글